检测鼠标按下时进入 QGraphicsItem

6
我希望能够在鼠标光标在按下鼠标按钮时移动到QGraphicsItem上时检测到它,也就是说,在鼠标进入该项之前按下了按钮。我的第一个想法是使用hoverEnterEvent,但似乎当左键按下时它不会触发。我的另一个想法是使用dragEnterEvent,但它似乎根本不会触发(即使我已经使用了setAcceptDrops(True))。
最好的方法是什么,可以检测到光标移动到项目顶部并按下鼠标按钮?

你可能需要在图形场景层面过滤鼠标事件,并向每个小部件添加鼠标悬停通知。不过,我本以为拖动事件应该可以工作。 - Caleb Huitt - cjhuitt
我面临着同样的问题。想听听答案。会检查拖动事件。 - problemofficer
2个回答

4

我刚刚发现这个问题,我知道它很老了,但我希望我的答案对于有这个问题的人是有用的。

QGraphicsViewQGraphicsScene派生类中重写mouseMoveEvent方法,并检查事件的buttons属性以了解当前按下了哪些按钮。以下是我正在进行的一个小项目中PyQt4的示例代码:

def mouseMoveEvent(self, event):
    buttons = event.buttons()
    pos = self.mapToScene(event.pos())
    object = self.scene().itemAt(pos)

    type = EventTypes.MouseLeftMove  if (buttons & Qt.LeftButton)  else\
           EventTypes.MouseRightMove if (buttons & Qt.RightButton) else\
           EventTypes.MouseMidMove   if (buttons & Qt.MidButton)   else\
           EventTypes.MouseMove

    handled = self.activeTool().handleEvent(type, object, pos)

    if (not handled):
        QGraphicsView.mouseMoveEvent(self, event)

0
尝试使用mouseMoveEvent()mousePressEvent()。如果它们无法帮助您,则需要重新实现虚拟方法。
bool QGraphicsItem::sceneEvent ( QEvent * event )

检查鼠标按钮状态并调用相应的事件处理程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接