QGraphicsItem - Follow Mouse Pointer

I am stuck on how to approach this. I have a QGraphicsItem inside a scene and I am passing the hover event from the scene to this child. While a move event is happening (I just use mouseMoveEvent with mouse tracking), I want another QGraphicsItem to follow the cursor.

I don't need collision detection, drag and drop, etc. Just the element that follows the cursor. The only two ways I can think of are ...

  • While the mouse is moving, draw a new QGraphicsItem at the mouse position. I need to clear the scene, redraw everything and draw a new position on top.
  • Somehow use an animation frame and whenever the mouse moves, animate the QGraphicsItem to jump to the new mouse position in 1 millisecond.

I might have either rethought this or didn't know of another way to do this ... any suggestions?


source to share

1 answer

I did it like this:

  • Create a GraphicsItem cursor

    to be moved with the mouse pointer and store its pointer somewhere (for example, in a subclass of a scene. I have a toolbox, so for me it is in one of these tools)
  • Set your Z-value ( QGraphicsItem::setZValue

    ) so that the cursor is painted over all other elements in your scene.
  • Track events QGraphicsScene::mouseMoveEvent

    , forward those events to the pointer, cursor

    and update the position of the position

what he. I think this matches your solution 1, except that you don't need to clear the scene thanks to the z-value function.



All Articles