Add event listener on QGraphicsEllipseItem

I'm creating a QGis Plugin in python which has aQGraphicsViewin which I draw and ellipse on which I want to detect if the user clicks.

I know the ellipse inheritsQGraphicsItemwhich has amouseReleaseEventbut I just don't know how to listen for an event.

self.scene = QGraphicsScene() // ellipse = self.dock.scene.addEllipse(0, 0, 100, 100, QPen(), QBrush()) // //now that i've got a reference to the ellipse how do I detect when someone clicks on it ?

I guess you have to override amouseReleaseEventinQGraphicsScene.

class MyScene(QGraphicsScene): def mouseReleaseEvent(self, event): # here you should do something with your scene if mouse button was released

then create scene with:

self.scene = MyScene()

Watch the video: CS50 2016 - Week 10 - Behind the Scenes (October 2021).