为什么mouseMoveEvent()无法跟踪event.button()的更改

时间:2019-04-23 06:27:29

标签: python pyqt

我正在尝试放大/缩小,但不仅限于wheelEvent。我想使用mouseMoveEvent进行缩放,就像在3d软件(Maya)中一样,当按下(Alt + Right Mouse)按钮并向下/向上或向左/向右移动鼠标时,它将放大/缩小。因此,我想采用event.pos()的坐标,但是除非单击鼠标右键,否则不需要进行缩放。所以我尝试做:

def mouseMoveEvent(self, event):
    modifierPressed = QApplication.keyboardModifiers( )
    if (modifierPressed & Qt.AltModifier) == Qt.AltModifier and event.button( ) == Qt.RightButton:
        print('Alt+RightClick')
        print(event.pos( ))

但是,我注意到event.button()总是返回NoButton。为什么会这样呢?如果有人想尝试运行它,它将放在完整的代码下面,但是代码需要图像。

from PySide2.QtGui import QPixmap, QBrush, QColor
from PySide2.QtCore import QSize, Qt, Signal, QPointF, QRect, QPoint
from PySide2.QtWidgets import QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene, QFrame, QSizePolicy, QGraphicsPixmapItem, QApplication, QRubberBand

_ui = {}
_ui['images_default'] = [
    ('a', r'imgA.png', QPointF(0, -200)),
    ('b', r'imgB.png', QPointF(0, -300)),
    ('c', r'imgC.png', QPointF(0, -400))
]
_ui['images_pressed'] = [
    ('a', r"imgD.png", QPointF(0, -200)),
    ('b', r"imgE.png", QPointF(0, -300)),
    ('c', r'imgF.png', QPointF(0, -400))
]


class MainWindow(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self)
        self.window = 'riga_gui'
        self.title = 'Character GUI'
        self.size = (1000, 650)

        self.create()

    def create(self):
        self.setWindowTitle(self.title)
        self.resize(QSize(*self.size))
        self.graphicsWidget = MainGraphicsWidget(self)

        self.mainLayout = QVBoxLayout()
        self.mainLayout.addWidget(self.graphicsWidget)
        self.setLayout(self.mainLayout)


class MainGraphicsWidget(QGraphicsView):
    def __init__(self, parent=None):
        super(MainGraphicsWidget, self).__init__(parent)
        self._scene = QGraphicsScene(backgroundBrush=Qt.gray)
        self.setScene(self._scene)

        for name, path, position in _ui['images_default']:
            _ui[name + '_buttonItem'] = QGraphicsPixmapItem(QPixmap(path))
            self._scene.addItem(_ui[name + '_buttonItem'])
            _ui[name + '_buttonItem'].setPos(position)

        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setBackgroundBrush(QBrush(QColor(30, 30, 30)))
        self.setFrameShape(QFrame.NoFrame)
        self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))


    def mouseMoveEvent(self, event):
        modifierPressed = QApplication.keyboardModifiers( )
        if (modifierPressed & Qt.AltModifier) == Qt.AltModifier and event.button( ) == Qt.RightButton:
            print('Alt+RightClick')
            print(event.button( ))
        elif (modifierPressed & Qt.AltModifier) == Qt.AltModifier:
            print('Alt')
        super(MainGraphicsWidget, self).mouseMoveEvent(event)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    window = MainWindow( )
    window.setGeometry(500, 300, 800, 600)
    window.show( )
    sys.exit(app.exec_( ))

1 个答案:

答案 0 :(得分:2)

根据Qt documentation请注意,对于鼠标移动事件,返回值始终为Qt :: NoButton。

改为使用QMouseEvent::buttons()

def mouseMoveEvent(self, event):
    modifierPressed = QApplication.keyboardModifiers( )
    if (modifierPressed & Qt.AltModifier) == Qt.AltModifier and event.buttons( ) == Qt.RightButton:
        print('Alt+RightClick')
        print(event.button( ))
    elif (modifierPressed & Qt.AltModifier) == Qt.AltModifier:
        print('Alt')
    else:
        print("Just move")
        print(event.buttons() == Qt.RightButton)
    super(MainGraphicsWidget, self).mouseMoveEvent(event)