I'm working on MAC OS X / Qt 5.3 widget application and I've run into a problem. I have QGraphicsView
containing scene with movable QGraphicsEllipseItem
, but I don't know how to catch the end of a move event.
What I'm trying to do is trigger a method every time the drag event of a point ends (and if that is not possible then when it moves), but I don't know where to start or what to do. I'd prefer not to make my own implementation of QGraphicsEllipseItem
, but if it's the only option I'm probably going to need a hand with it.
The problem is also I don't know where to listen to these events, I have a class that inserts the points into the widget and generally serves as a code-behind for it and I'd like to do the catching there since the method is accessible from there and gets specific data from it. I'm completely at loss of what to do or how to do it.
I don't want complete code, but I'd appreciate an example, if you can (with placement specified).
As @SebastianLange commented, you need to handle the mouse events. There are two ways of doing this, based upon the same method.
A QGraphicsItem
supports the following functions:
virtual void mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event )
virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
The first method would be to inherit from QGraphicsEllipseItem
and override the mouseEvent
s, which would allow you to know when the item has been moved and a call in the mouseReleaseEvent
signals that the move has ended.
If you don't want to inherit from QGraphicsEllipseItem
, another method is to install a scene event filter
This involves creating a separate object derived from QGraphicsItem
, overloading the mouse events (as above) and then installing this object as the event filter for the QGraphicsEllipseItem
object.
Personally, I'd recommend using the first method and inherit from QGraphicsEllipseItem
.