在 QGraphicsScene 类中跟踪鼠标移动
我对 QGraphicsScene 进行了子类化,并添加了方法 mouseMoveEvent
来处理鼠标移动事件。
我在 QGraphicsView 顶部创建了一个标尺,并让标尺跟踪鼠标移动。在 QGraphicsScene::mousemoveEvent 中,我显式调用标尺小部件的 mouseMoveEvent 。目的是让标尺知道当前鼠标位置。
现在看来,当我移动鼠标时,QGraphicsScene::mousemoveEvent
没有被调用。但是,如果我按下鼠标左键并在按住按钮的同时移动它,我就可以让它工作。
这不是我愿意看到的;我希望每当我将鼠标放在视图上并移动鼠标时都会调用此方法。
有什么解决方法吗?
I subclassed QGraphicsScene and added the method mouseMoveEvent
to handle mouse move events.
I created a ruler on top of QGraphicsView and have the ruler tracking mouse movement. In QGraphicsScene::mousemoveEvent
, I call mouseMoveEvent
of the ruler widget explicitly. The purpose is to have the ruler aware of the current mouse position.
Now it seems that QGraphicsScene::mousemoveEvent
is not called when I move the mouse. However, I can get it to work if I press the left mouse button and move it while holding the button.
This is not what I'd like to see; I'd like this method to be called whenever I place the mouse over the view and move the mouse.
Is there any workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如
QGraphicsView
文档中所述,视图负责将鼠标和键盘事件转换为场景事件并将其传播到场景:由于鼠标移动事件仅在默认情况下按下按钮时发生,你需要
setMouseTracking(true)
在视图上首先生成移动事件,以便它可以将这些事件转发到场景。或者,如果您不需要转换为场景坐标,您可以直接在视图中而不是在场景中重新实现
mouseMoveEvent
。但在这种情况下,请确保调用基类QGraphicsView:: mouseMoveEvent
在您的实现中,以便为场景中的项目正确生成悬停事件。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: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 classQGraphicsView::mouseMoveEvent
in your implementation, so that hover events are properly generated for the items in your scene.tgs.cpp
:tgs.h
:tgs.cpp
:tgs.h
: