我有一个QGraphicsScene,QGraphicsView和一些QGraphicsItems子类。我想跟踪光标最顶部可见的项目。在大多数情况下,使用 hoverEnterEvent 可以正常工作,但是如果我有两个对象,其中一个在另一个之上,它确实可以同时输入两个对象,但不能离开内部对象(并重新进入外部,因为它从未离开过外部(。
+-------------------------------------+
outside | |
| outer |
| |
| |
| +-------------+ | +-------------+
| | | | | |
| | | | | another |
| | inner | | | |
| | | | | |
| | | | +-------------+
| +-------------+ |
| |
| |
| |
| |
+-------------------------------------+
outside -> outer : works, outer is selected
outside -> outer -> outside -> another : works, first outside is selected, than nothing, than another
outer -> inner : works, inner is seletected
outside -> outer -> inner -> outer: does not work, first outside is selected, than inner, but than nothing (should be outer again)
除了循环遍历所有通过 hoverLeaveEvent 上的轻微延迟单次触发的图形项之外,我还能做什么?
编辑:我找到了一个临时解决方案:我在 MyQGraphicsItem *>
您的QGraphicsItem
位于一个可以由一个或多个QGraphicsView
显示的QGraphicsScene
中。
我知道模型视图映射通常是 1:1。
我仍然建议在您看来实现这样的鼠标处理,而不是在场景中:
在graphicsView->viewport()
上安装eventFilter
。
重写筛选器类中的 eventFilter()
函数。
注意QEvent::MousePress
,MouseMove
,MouseRelease
,也许Enter
和Leave
,这取决于你需要什么。
可能需要在视口上setMouseTracking(true)
。
然后,在事件过滤功能中,使用 QGraphicsView::mapToScene()
和 QGraphicsScene::itemAt()
查找最上面的项目,或::items()
查找光标下的所有项目。
我最近用它来通过在视图上绘画来用边框装饰最顶层的项目 ( QGraphicsView::drawForeground()
(。