Tracking mouse move in QGraphicsScene class

后端 未结 2 839
栀梦
栀梦 2021-02-18 15:36

I subclassed QGraphicsScene and added method mouseMoveEvent to handle mouse move event. I created a ruler on top of GraphicsView and have the ruler tracking mouse movement. In t

2条回答
  •  情歌与酒
    2021-02-18 15:48

    As stated in the QGraphicsView documentation, the view is responsible for translating mouse and keyboard events into scene events and propagating that to the scene:

    You can interact with the items on the scene by using the mouse and keyboard. QGraphicsView translates the mouse and key events into scene events, (events that inherit QGraphicsSceneEvent,), and forward them to the visualized scene.

    Since mouse move events only occur when a button is pressed by default, you need to setMouseTracking(true) on the view for the move events to generated in the first place, so that it can forward those to the scene.
    Alternatively, if you don't need the translation to scene coordinates, you could reimplement the mouseMoveEvent in the view directly rather than in your scene. But in this case, make sure you call the base class QGraphicsView::mouseMoveEvent in your implementation, so that hover events are properly generated for the items in your scene.

提交回复
热议问题