The middle mouse button drags the scene to move freely and is related to DragMode

! I don’t fully understand how it works,

  def mousePressEvent(self, event: QMouseEvent):
        """Dispatch Qt's mousePress event to corresponding function below"""
        if event.button() == Qt.MiddleButton:
            self.middleMouseButtonPress(event)
        else:
            super().mousePressEvent(event)

    def mouseReleaseEvent(self, event: QMouseEvent):
        """Dispatch Qt's mouseRelease event to corresponding function below"""
        if event.button() == Qt.MiddleButton:
            self.middleMouseButtonRelease(event)
        else:
            super().mouseReleaseEvent(event)

    def middleMouseButtonPress(self, event):
        releaseEvent = QMouseEvent(QEvent.MouseButtonRelease, event.localPos(), event.screenPos(),
                                   Qt.LeftButton, Qt.NoButton, event.modifiers())
        super().mousePressEvent(releaseEvent)
        self.setDragMode(QGraphicsView.ScrollHandDrag)
        fakeEvent = QMouseEvent(event.type(), event.localPos(), event.screenPos(), Qt.LeftButton,
                                event.buttons() | Qt.LeftButton, event.modifiers())
        super().mousePressEvent(fakeEvent)

    def middleMouseButtonRelease(self, event):
        print('MMB')

Insert picture description here
Insert picture description here
Insert picture description here
QGraphixcsView attribute Insert picture description here
DragMode Chinese interpretation: This attribute retains the behavior of dragging the mouse onto the scene when the left mouse button is pressed.
This property defines what should happen when the user clicks on the background of the scene and drags the mouse (for example, using a hand cursor to scroll the viewport content or using a rubber band to select multiple items). The default value NoDrag does not perform any operation.
This behavior only affects mouse clicks that are not processed by any item. You can define custom behaviors by creating a subclass of QGraphicsView and reimplementing mouseMoveEvent().

Guess you like

Origin blog.csdn.net/angelsweet/article/details/114313578