检测鼠标何时按下按钮进入 QGraphicsItem

发布于 2024-09-25 11:51:45 字数 255 浏览 1 评论 0原文

我想在按下鼠标按钮时检测鼠标光标何时移入 QGraphicsItem 上,即在鼠标进入项目之前按下按钮。我的第一个想法是使用 hoverEnterEvent,但按下鼠标左键时似乎没有触发。我的另一个想法是使用dragEnterEvent,但它似乎根本没有触发(即使我已经使用setAcceptDrops(True)

检测的最佳方法是什么当光标移动到项目顶部并按下鼠标按钮时?

I want to detect when the mouse cursor moves in over a QGraphicsItem while a mouse button is pressed, i.e. the button is pressed before the mouse enters the item. My first idea was to use hoverEnterEvent, but it doesn't seem to trigger when the left mouse button is pressed. My other idea was to use dragEnterEvent, but it doesn't seem to trigger at all (even though I have used setAcceptDrops(True).

What is the best way to detect when the cursor moves on top of an item and the mouse button is pressed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2024-10-02 11:51:45

我刚刚发现这个问题,我知道它很旧,但我希望我的答案对遇到这个问题的人有用。

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)

I just found this question, I know it's old, but I hope my answer will be useful for someone with this problem.

In the QGraphicsView or QGraphicsScene derived class override the mouseMoveEvent method and check the event's buttons property to know what buttons are currently pressed. Here's an example code in PyQt4 from a small project I'm working on:

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)
屋顶上的小猫咪 2024-10-02 11:51:45

尝试使用 mouseMoveEvent()mousePressEvent()。如果它们不能帮助您,那么您需要重新实现虚拟方法,

bool QGraphicsItem::sceneEvent ( QEvent * event )

检查内部的鼠标按钮状态并调用适当的事件处理程序。

Try the mouseMoveEvent() and mousePressEvent(). If they do not help you then you'll need to reimplement virtual method

bool QGraphicsItem::sceneEvent ( QEvent * event )

Check the mouse button state inside and call the appropriate event handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文