Mukesh kumar
Mukesh kumar

Reputation: 23

QGraphicsScene Mouse Event

I'm working with QGraphicsScene class. How can I detect the mouse leaving from the QGraphicsScene ? Is there any internal function for this ?

Upvotes: 2

Views: 293

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

If you want to detect the release event you must override the mouseReleaseEvent() method:

from PyQt5 import QtCore, QtGui, QtWidgets


class GraphicsScene(QtWidgets.QGraphicsScene):
    def mouseReleaseEvent(self, event):
        print(event.scenePos())
        super().mouseReleaseEvent(event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    scene = GraphicsScene()
    w = QtWidgets.QGraphicsView(scene)
    w.resize(640, 480)
    w.show()

    sys.exit(app.exec_())

Update:

The QGraphicsScene itself is not an element that allows to detect when you leave it since this can be part of several QGraphicsView, what you must do is detect when you leave the QGraphicsView override the leaveEvent() method:

from PyQt5 import QtCore, QtGui, QtWidgets


class GraphicsView(QtWidgets.QGraphicsView):
    def enterEvent(self, event):
        print("enter")
        super().enterEvent(event)

    def leaveEvent(self, event):
        print("leave")
        super().leaveEvent(event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    scene = QtWidgets.QGraphicsScene()
    w = GraphicsView(scene)
    w.resize(640, 480)
    w.show()

    sys.exit(app.exec_())

Upvotes: 1

Related Questions