Here is an example of a simple PyQt5 Program, that adds a QGraphicsScene and QGraphicsView and then adds a custom EllipseItem. That EllipseItem inherits from QGraphicsEllipseItem and does nothing more than to implement the mousePressEvent() and mouseReleaseEvent() methods.
from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView, QGraphicsEllipseItem, \
QGraphicsItem, QGraphicsSceneMouseEvent
import sys
class EllipseItem(QGraphicsEllipseItem):
def __init__(self):
super().__init__(0, 0, 100, 100)
# self.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
# self.setFlags(QGraphicsItem.ItemIsMovable)
def mousePressEvent(self, event: 'QGraphicsSceneMouseEvent') -> None:
super().mousePressEvent(event)
print("Mouse Pressed")
def mouseReleaseEvent(self, event: 'QGraphicsSceneMouseEvent') -> None:
super().mouseReleaseEvent(event)
print("Mouse Released")
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.title = "mouse Release"
self.width = 800
self.height = 600
# Init UI
self.canvas = QGraphicsScene()
self.graphics_view = QGraphicsView(self.canvas, self)
self.graphics_view.setGeometry(0, 0, self.width, self.height)
self.canvas.addItem(EllipseItem())
self.setWindowTitle(self.title)
self.setGeometry(0, 0, 800, 600)
self.show()
def main():
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec())
if __name__ == '__main__':
main()
My Problem is, that the mouseReleaseEvent() method is not called as expected, it depends on the implementation of mousePressEvent() and the flags that are set.
When only printing, without calling super().mousePressEvenet(event), all works fine, mouseReleaseEvent() is called:
def mousePressEvent(self, event: 'QGraphicsSceneMouseEvent') -> None:
print("Mouse Pressed")
Output when clicking on the Ellipse:
Mouse Pressed
Mouse Released
When Adding the call to the super method, mouseReleaseEvent() is not called:
def mousePressEvent(self, event: 'QGraphicsSceneMouseEvent') -> None:
super().mousePressEvent(event)
print("Mouse Pressed")
Output when clicking on the Ellipse:
Mouse Pressed
When setting the QGraphicsItem.ItemIsMovable or QGraphicsItem.ItemIsSelectable Flag in init(), mouseReleaseEvent() is called and everything is fine. The thing is, I don't want the Item to be movable or selectable, I need to implement those methods myself.
Can I set some other option, so that the mouseReleaseEvent() is called as expected?
Okay, I found a fix that works for me.
@musicamante gave me the Idea, not to call super().mousePressEvent(event)
.
I had discarded that idea previously, because my class in question inherits from another custom class. Its mousePressedEvent()
method is not called automatically, so I need to call super().mousePressedEvent()
for it to trigger.
The trick for me is, to call super.mousePressedEvent()
only, when I'm not inheriting from a build-in class, like QGraphicsEllipseItem
. In the class, that inherits directly from a build-in class, I have my functionality, but don't call super().mousePressedEvent()
(I did the same thing for all other mouseEvents).