Pyside QgraphicsScene无法捕获鼠标事件

Ale*_*lex 0 qt pyqt pyside qt-events qt-signals

我找不到将鼠标事件附加到场景的方法。如果没有 View,所有事件都会被捕获,但是当注释掉时,只有 mousePressEvent 起作用。请帮忙。

from PySide import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.Scene()
        self.View()

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

    def mouseMoveEvent(self, event):
        print "moving....."

    def mouseReleaseEvent(self, event):
        print "-------released"

    def Scene(self):
        self.s = QtGui.QGraphicsScene(self)

    def View(self):
        self.v = QtGui.QGraphicsView(self.s)
        self.setCentralWidget(self.v)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.resize(300, 200)
    window.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

Ava*_*ris 5

在 Qt 中,事件是从子级到父级处理的。首先,孩子获得事件。然后它决定是否处理该事件。如果它不想对它采取行动,它可以处理ignore该事件,并且该事件将被传递给父级。

在您的设置中,您将 aQMainWindow作为父项,将 aQGraphicsView作为其子项。上的每个事件都QGraphicsView将由QGraphicsView第一个事件处理。如果它不想要该事件,它就会将ignore其传递给QMainWindow.

为了更好地可视化它,子类化QGraphicsView并覆盖它的mouse*Events:

from PySide import QtGui, QtCore

class View(QtGui.QGraphicsView):
    def mousePressEvent(self, event):
        print "QGraphicsView mousePress"

    def mouseMoveEvent(self, event):
        print "QGraphicsView mouseMove"

    def mouseReleaseEvent(self, event):
        print "QGraphicsView mouseRelease"

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.Scene()
        self.View()

    def mousePressEvent(self, event):
        print "QMainWindow mousePress"

    def mouseMoveEvent(self, event):
        print "QMainWindow mouseMove"

    def mouseReleaseEvent(self, event):
        print "QMainWindow mouseRelease"

    def Scene(self):
        self.s = QtGui.QGraphicsScene(self)

    def View(self):
        self.v = View(self.s)
        self.setCentralWidget(self.v)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.resize(300, 200)
    window.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

你会看到如下输出:

QGraphicsView mousePress
QGraphicsView mouseMove
...
QGraphicsView mouseMove
QGraphicsView mouseRelease
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,只有获取view才能“查看”事件,因为view不会选择传递事件。

或者,您可以选择忽略QGraphicsView. 这就像在说“我对此不做任何事情,让别人来处理”。该事件将传递给父级以供其选择要执行的操作:

class View(QtGui.QGraphicsView):
    def mousePressEvent(self, event):
        print "QGraphicsView mousePress"
        # ignore the event to pass on the parent.
        event.ignore()

    def mouseMoveEvent(self, event):
        print "QGraphicsView mouseMove"
        event.ignore()

    def mouseReleaseEvent(self, event):
        print "QGraphicsView mouseRelease"
        event.ignore()
Run Code Online (Sandbox Code Playgroud)

和输出:

QGraphicsView mousePress
QMainWindow mousePress
QGraphicsView mouseMove
QMainWindow mouseMove
...
QGraphicsView mouseMove
QMainWindow mouseMove
QGraphicsView mouseRelease
QMainWindow mouseRelease
Run Code Online (Sandbox Code Playgroud)

现在,您可以看到view首先获取事件。但由于它ignore是事件,它才会被传递到主窗口,并且只有在QMainWindow接收到信号之后。

长话短说,不用担心。您view将接收事件并对其采取适当的行动。