3
votes

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?

1
Just to maybe clarify a bit better - essentially I'm making a tooltip out of a QGraphicsItem that fires (and follows the cursor) while hovering over another QGraphicsItemgiraffee

1 Answers

3
votes

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.