Results 1 to 7 of 7

Thread: Draw a line between widget and mouse?

  1. #1
    Join Date
    Apr 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Draw a line between widget and mouse?

    Hi,

    I need to draw a line between a widget and the current mousePosition.

    Let's say I have 3 QPushButtons.. and when I click on one of them, I want to drag a red line with the mouse, starting at the Button - to "connect" it to something..

    Normaly I would do this with a QGraphicsView and just paint a button inside of the View, but because of the architecture of our Software, that's not possible..

    So, is there any way to just draw a line as Screen-Overlay or something? The Button and the Target for the line may not be on the same parent-widget.. could be even in two different windows.

    An alternative way would be do do something like drag'n'drop: Drag the Button and drop it on the Target.. but therefore I'd like to have a visual representation of the button under the mouse cursor while dragging (like in Windows Explorer when dragging files).. is there any way to do this?

    Thanks,
    Aya

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Draw a line between widget and mouse?

    You can use QStyle::drawPrimitive to draw your buttons and render all of your scene by QPainter

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw a line between widget and mouse?

    You can override paintevent of your main window and render your line in there.
    This would be like overlay painting, and you would need to calculate your widgets positions accordingly..

  4. #4
    Join Date
    Apr 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Draw a line between widget and mouse?

    Quote Originally Posted by borisbn View Post
    You can use QStyle::drawPrimitive to draw your buttons and render all of your scene by QPainter
    Since I have many widgets in this App, I think this wouldn't be a ideal solution.

    Quote Originally Posted by aamer4yu View Post
    You can override paintevent of your main window and render your line in there.
    This would be like overlay painting, and you would need to calculate your widgets positions accordingly..
    Mhh.. I just tried this, but my line is behind all other widgets in the window..

    Aya~

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw a line between widget and mouse?

    Mhh.. I just tried this, but my line is behind all other widgets in the window..
    Did you call the base class paintEvent after drawing the line

  6. #6
    Join Date
    Apr 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Draw a line between widget and mouse?

    Quote Originally Posted by aamer4yu View Post
    Did you call the base class paintEvent after drawing the line :rolleyes:
    No, I didn't call the QMainWindow::paintEvent(ev) at all... :/

  7. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw a line between widget and mouse?

    Try calling the base class paint event first and then do your drawing.
    See if it works.

Similar Threads

  1. Draw a line
    By Daan in forum KDE Forum
    Replies: 1
    Last Post: 27th August 2009, 17:29
  2. Draw Line
    By aloysiusjegan in forum Qwt
    Replies: 4
    Last Post: 12th February 2009, 11:02
  3. Draw Line :confused:
    By sabeesh in forum Qt Programming
    Replies: 9
    Last Post: 24th January 2008, 11:26
  4. Best way to draw a Line
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2008, 09:57
  5. Draw a line in a Label
    By sabeesh in forum Qt Programming
    Replies: 4
    Last Post: 23rd July 2007, 13:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.