Вопрос

I can't find the way to attach mouse events to scene. Without View all events are capcured, but when commented out, only mousePressEvent works. Please, help.

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_())
Это было полезно?

Решение

In Qt, events are handled from child to parent. First the child gets the event. Then it decides whether or not it'll handle the event. If it doesn't want to act on it, it can ignore the event and the event will be passed to the parent.

In your setup, you have QMainWindow as the parent, and a QGraphicsView as its child. Every event on the QGraphicsView will be handled by the QGraphicsView first. If it doesn't want the event, it will ignore it and pass on to the QMainWindow.

To visualize it better, subclass QGraphicsView and override its 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_())

And you'll see an output like:

QGraphicsView mousePress
QGraphicsView mouseMove
...
QGraphicsView mouseMove
QGraphicsView mouseRelease

As you can see, only the view gets to 'see' the events, because view doesn't choose to pass the events.

Alternatively, you can choose to ignore those events in the QGraphicsView. It's like saying 'I don't do anything with this, let someone else take care of it'. And the event will be passed to the parent for it to choose what to do:

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()

And the output:

QGraphicsView mousePress
QMainWindow mousePress
QGraphicsView mouseMove
QMainWindow mouseMove
...
QGraphicsView mouseMove
QMainWindow mouseMove
QGraphicsView mouseRelease
QMainWindow mouseRelease

Now, you can see that view gets the event first. But since it ignores the event, it's passed to the main window and only after that QMainWindow receives the signal.

Long story short, don't worry. Your view will receive events and act on them just fine.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top