[SOLVED] QGraphicsItem – Follow the mouse cursor


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…

  1. 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.
  2. 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

  1. 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)
  2. Set its Z-Value (QGraphicsItem::setZValue) so that the cursor will be painted above all other items in your scene
  3. Track QGraphicsScene::mouseMoveEvent events, forward these events down to the cursor 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)

Leave a Reply

Your email address will not be published. Required fields are marked *