PDA

View Full Version : Draw a line between widget and mouse?



AyaKoshigaya
21st April 2010, 15:53
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

borisbn
21st April 2010, 19:16
You can use QStyle::drawPrimitive (doc.qt.nokia.com/4.6/qstyle.html) to draw your buttons and render all of your scene by QPainter

aamer4yu
22nd April 2010, 05:48
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..

AyaKoshigaya
22nd April 2010, 10:22
You can use QStyle::drawPrimitive (doc.qt.nokia.com/4.6/qstyle.html) 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.


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~

aamer4yu
22nd April 2010, 10:47
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 :rolleyes:

AyaKoshigaya
22nd April 2010, 10:59
Did you call the base class paintEvent after drawing the line :rolleyes:

No, I didn't call the QMainWindow::paintEvent(ev) at all... :/

aamer4yu
26th April 2010, 06:32
Try calling the base class paint event first and then do your drawing.
See if it works.