Issue
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?
Solution
I did it like this
- Create the GraphicsItem
cursor
which 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 - Track
QGraphicsScene::mouseMoveEvent
events, forward these events down to thecursor
pointer, and update item’s position
that’s it.
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)