In my project I'm using a QGraphicsScene and adding / removing items all over the code.
Now I want to get notified whenever a QGraphicsItem gets added or removed. Many Qt classes have notification signals or at least virtual functions that get called on such changes. I'm trying to avoid adding many rows of code in many places which is not only cumbersome but also unsafe (forgetting an insert / removal now or in the future).
I need a solution that works with any QGraphicsItem.
This is a list of things that do not work:
- connect to a signal of
QGraphicsScene(as inQAbstractItemModel::rowsInserted()) -> there is none. - inherit from
QGraphicsSceneand overload a virtual notifier function (as inQTabWidget::tabInserted()) -> there is none. - inherit and overload
addItem(), sending notifications myself (as inQMdiArea::addSubWindow()) ->addItemisn't virtual and gets called from the constructors ofQGraphicsItems. - install event filters on newly added
QGraphicsItems-> no idea how to get the newly added item AND it would be asceneEventFilterwhich can only be installed on otherQGraphicsItems. - connect to
itemChange()ofQGraphicsItem->itemChangeis not a signal AND overloadingQGraphicsItemis not an option. - wrap
QGraphicsScene(having the scene as a private member) and only expose functionsaddItemandremoveItem-> butQGraphicsItemsin that scene could still access it viascene()function, so this solution is not safe enough.
How can I get notifications on item changes?
If there is an easy way that I simply missed, please point me to it. Else, I'd appreciate ideas on this very much.