1

I want to catch mouse events for some QGraphicsItem. When the item is added directly to a QGraphicsScene, everything works as expected: when using option 1 below, the console prints "foo" when the user clicks within the rectangle.

However, if the item is added indirectly via a group, it does not receive events anymore (option 2 below). It seems the event chain is broken that way. I tried to set scene as the parent to the QGraphicsItem to restore the chain but it results in an error, obviously I am not doing it the right way?

What is the correct way to add a QGraphicsItem to a group and still receive mouse events?

from PyQt5.QtWidgets import QApplication, QGraphicsRectItem, QGraphicsScene, QGraphicsView, QMainWindow


class Rect(QGraphicsRectItem):
  def mousePressEvent(self, event):
    print("foo")


app = QApplication([])
window = QMainWindow()
window.setGeometry(100, 100, 400, 400)
view = QGraphicsView()
scene = QGraphicsScene()

rect = Rect(0, 0, 150, 150)

# Option 1.
# scene.addItem(rect)  # works fine, prints 'foo' when clicked
# Option 2.
group = scene.createItemGroup([rect])  # no mouse event received by rect

view.setScene(scene)
window.setCentralWidget(view)
window.show()
app.exec()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
user209974
  • 1,737
  • 2
  • 15
  • 31
  • 1
    What would you like to achieve with the group specifically? – ypnos Mar 01 '20 at 16:00
  • Does this answer your question? [Events with QGraphicsItemGroup](https://stackoverflow.com/questions/3021441/events-with-qgraphicsitemgroup) – ypnos Mar 01 '20 at 16:01
  • 1
    @ypnos setHandlesChildEvents is not available for Qt5 – eyllanesc Mar 01 '20 at 16:04
  • @ypnos Moving items together. – user209974 Mar 01 '20 at 16:49
  • @user209974 I understand that you want to move all items together using the mouse, if so, in what cases do you want to use the mousePressEvent of the items and for what? – eyllanesc Mar 01 '20 at 17:02
  • @eyllanesc For doing stuff probably not relevant to a minimal, complete and reproducible example. – user209974 Mar 01 '20 at 17:04
  • mmm, what I suspect is that you have an XY problem ..., and I was thinking of proposing an answer without using QGraphicsItemGroup but I don't know if that will work for you since it could cancel other things – eyllanesc Mar 01 '20 at 17:06
  • 1
    I will be more precise, when you want to move several items together it is normal to use the mouse + rubberband to select those items, and then at that time you can add the items to the group, then move them and before a certain action could be removed the items of the group, that is to say limit the action of the group only when necessary and thus the item will not be affected by the side effect. – eyllanesc Mar 01 '20 at 17:13

1 Answers1

2

If the only objective of the use of QGraphicsItemGroup is to enable the movement of a group of items then the procedure is to select the items, add them to the group, move the items and before any action remove the items from the group. Thus, it will not be necessary for the items to permanently belong to the group but only when necessary avoiding side effects such as the non-transmission of events.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241