python - 为什么 mouseMoveEvent() 不跟踪 event.button() 的变化

标签 python pyqt

我正在尝试放大/缩小,但不仅仅是使用wheelEvent。我想使用 mouseMoveEvent 并进行类似于 3d 软件(Maya)中的缩放,当按下(Alt+鼠标右键)按钮并且鼠标向下/向上或向左/向右移动时,它将放大/缩小。所以我想获取 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_( ))

最佳答案

根据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)

关于python - 为什么 mouseMoveEvent() 不跟踪 event.button() 的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55805454/

相关文章:

python - PySide 中的样式表不起作用

python - 是否有等同于wx.FutureCall(在窗口初始化和绘制后调用函数)的PyQT?

python - 深度复制嵌套迭代(或改进的 itertools.tee 用于迭代的迭代)

python - 为 Windows 构建 Vulkan 工具

python - 使用 Selenium 清除html输入框

python - 从pyqt中的框架中删除所有内容

python - Telnet输出解析

python - 在 MAC 10.7 或 10.8 上强制使用 32 位 Python

python - 清除pyqt布局中的所有小部件

python - 在 PyQt 中命名 getter 和属性