【问题标题】:Detect when mouse enter QGraphicsItem with button down检测鼠标何时按下按钮进入 QGraphicsItem
【发布时间】:2011-04-20 05:24:04
【问题描述】:

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

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

【问题讨论】:

  • 您可能必须在图形场景级别过滤鼠标事件,并为每个小部件添加一些通知,以告知鼠标位于上方。但是,我原以为拖动事件应该起作用。
  • 我也面临同样的问题。想听听答案。将结帐拖动事件。

标签: qt qgraphicsitem


【解决方案1】:

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

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)

【讨论】:

    【解决方案2】:

    试试mouseMoveEvent()mousePressEvent()。如果它们对您没有帮助,那么您需要重新实现虚拟方法

    bool QGraphicsItem::sceneEvent ( QEvent * event )
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多