I’m stuck on how to approach this. I have a QGraphicsItem within a scene, and I’m passing a hover event from the scene to this child. While the move event occurs (I’m just using mouseMoveEvent with mouse tracking on), I want another QGraphicsItem to follow the cursor.
I don’t need any collision detection, drag and drop, etc. Just an item that follows the cursor. The only two ways I can think of are…
- While the mouse moves, draw a new QGraphicsItem at the mouse position. I would need to clear the scene, redraw everything, and draw the new position on top.
- Somehow use the animation framework and whenever the mouse moves, animate the QGraphicsItem to move to the new mouse position in 1 millisecond.
I’m probably either overthinking this or unaware of another way to do this… any suggestions?
I did it like this
- Create the GraphicsItem
cursorwhich will be moved with mouse-cursor, and store its pointer somewhere (in scene subclass for instance. I have a toolset, so for me it’s in one of these tools)
- Set its Z-Value (
QGraphicsItem::setZValue) so that the cursor will be painted above all other items in your scene
QGraphicsScene::mouseMoveEventevents, forward these events down to the
cursorpointer, and update item’s position
I guess it corresponds to your solution 1, except you don’t have to clear the scene thanks to z-value feature.
Answered By – azf
Answer Checked By – Robin (BugsFixing Admin)