Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QGraphicsWidget's mouse drag.

Tags:

c++

qt

How to implement mouse draging on QGraphicsWidget?

I tried with

setFlag(QGraphicsItem::ItemIsMovable);

And that works sometime (?)

enter image description here

For example, node 6 I can move with mouse, but rest of nodes wont move.

Also, how can I implement that when I move some node, edge that attached to that node move with it.

like image 737
Steva Avatar asked Nov 30 '25 08:11

Steva


1 Answers

The real reason may be that you have not provided an override of the shape() function. This function is used to define the "collision" geometry: the parts of the object that you can click on. If you inherrit from QGraphicsItem directly, it all seems to work fine without providing a definition, but QGraphicsWidget overrides the shape() function itself and simply returns QWidget::rect() (which may also not be defined in your class), so the default implementation from QGraphicsItem isn't being used anymore when you inherit from QGraphicsWidget!

If you provide a definition of the shape function that returns an ellipse that surrounds the circle you draw for your node, I bet it just might start working. For example:

QPainterPath MyGraphicsWidget::shape() const
{
    QPainterPath path;
    path.addEllipse(QPointF(0, 0), 25, 25);
    return path;
}

This may be an old question, but I just spent 3 hours trying to figure this out, myself. Hopefully this answer is useful to someone out there if not you ;)

like image 137
Jordan Melo Avatar answered Dec 01 '25 21:12

Jordan Melo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!