为什么mouseMoveEvent()不跟踪事件按钮()

2024-06-28 19:36:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: nameselfeventuipngdefbuttonalt
1条回答
网友
1楼 · 发布于 2024-06-28 19:36:51

根据Qt documentation:注意,对于mouse move事件,返回值总是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)

相关问题 更多 >