【问题标题】:Why mouseMoveEvent does nothing in PyQt5为什么 mouseMoveEvent 在 PyQt5 中什么都不做
【发布时间】:2016-06-29 18:14:17
【问题描述】:

我尝试在 PyQt5 和 Python3.5 中使用 mouseMoveEvent 和 mousePressEvent,但是当我单击鼠标时什么也没有。我的代码如下,有问题吗?

from PyQt5 import QtWidgets, QtGui, QtCore

class Window(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        widget = QtWidgets.QWidget(self)
        layout = QtWidgets.QVBoxLayout(widget)
        self.graphicsView = QtWidgets.QGraphicsView()
        self.graphicsView.setCursor(QtCore.Qt.CrossCursor)
        self.graphicsView.setObjectName("graphicsView")
        layout.addWidget(self.graphicsView)
        self.setCentralWidget(widget)

    def mouseMoveEvent(self, event):
        if event.buttons() == QtCore.Qt.NoButton:
            print("Simple mouse motion")
        elif event.buttons() == QtCore.Qt.LeftButton:
            print("Left click drag")
        elif event.buttons() == QtCore.Qt.RightButton:
            print("Right click drag")

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            print("Press!")

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python python-3.x pyqt pyqt5


    【解决方案1】:

    我确信您的事件是在 QGraphicsView 内部处理的。您必须阅读有关事件传播的更多信息。尝试在 Window 之上不添加任何其他小部件。并且不要忘记 abt MouseTracking 属性,该属性默认为 false,并且根本不会发生没有按钮的鼠标移动事件。

    我会推荐阅读this 文章。它已经很老了,但仍然很重要。 QGraphicsView 中的鼠标事件也以不同的方式处理,请阅读 docs 了解更多详细信息。

    由于我是 C++ 开发人员,所以没有代码示例。

    【讨论】:

      【解决方案2】:

      首先,您必须启用mouse-tracking:

              self.graphicsView.setMouseTracking(True)
      

      那么你可以使用QGraphicsView的子类:

      class GraphicsView(QtWidgets.QGraphicsView):   
          def mouseMoveEvent(self, event):
              if event.buttons() == QtCore.Qt.NoButton:
                  print("Simple mouse motion")
              elif event.buttons() == QtCore.Qt.LeftButton:
                  print("Left click drag")
              elif event.buttons() == QtCore.Qt.RightButton:
                  print("Right click drag")
              super(GraphicsView, self).mouseMoveEvent(event)
      
          def mousePressEvent(self, event):
              if event.button() == QtCore.Qt.LeftButton:
                  print("Press!")
              super(GraphicsView, self).mousePressEvent(event)
      

      或者安装一个事件过滤器:

              self.graphicsView.viewport().installEventFilter(self)
              ...
      
          def eventFilter(self, source, event):
              if event.type() == QtCore.QEvent.MouseMove:
                  if event.buttons() == QtCore.Qt.NoButton:
                      print("Simple mouse motion")
                  elif event.buttons() == QtCore.Qt.LeftButton:
                      print("Left click drag")
                  elif event.buttons() == QtCore.Qt.RightButton:
                      print("Right click drag")
              elif event.type() == QtCore.QEvent.MouseButtonPress:
                  if event.button() == QtCore.Qt.LeftButton:
                      print("Press!")
              return super(Window, self).eventFilter(source, event)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-07
        • 1970-01-01
        • 2018-07-13
        • 2019-04-05
        • 1970-01-01
        • 1970-01-01
        • 2017-12-08
        相关资源
        最近更新 更多