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:
QGraphicsScene
(as in QAbstractItemModel::rowsInserted()
) -> there is none.QGraphicsScene
and overload a virtual notifier function (as in QTabWidget::tabInserted()
) -> there is none.addItem()
, sending notifications myself (as in QMdiArea::addSubWindow()
) -> addItem
isn't virtual and gets called from the constructors of QGraphicsItems
.QGraphicsItems
-> no idea how to get the newly added item AND it would be a sceneEventFilter
which can only be installed on other QGraphicsItems
.itemChange()
of QGraphicsItem
-> itemChange
is not a signal AND overloading QGraphicsItem
is not an option.QGraphicsScene
(having the scene as a private member) and only expose functions addItem
and removeItem
-> but QGraphicsItems
in that scene could still access it via scene()
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.
I think the best you can do is to connect to QGraphicsScene::changed()
signal.
It will not tell you what items are changed/added/removed since it's intended for QGraphicsView
to update the display. But you should be able to find out using the supplied regions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With