PDA

View Full Version : How to tell when items are added to or removed from a QGraphicsScene?



wayfaerer
5th March 2012, 19:01
I'd like to know when items are added to or removed from a QGraphicsScene from within the scene. Is there an easy way to do that, or do I have to do it in the constructor and destructor of the QGraphicsItems?

In particular, I'd like to emit a signal whenever an item is added or removed so that I can use setWindowModified(true) in my QMainWindow.

wysota
5th March 2012, 19:26
Constructing an item doesn't mean it gets added to the scene the same as removing items from the scene doesn't mean they get destroyed. You can monitor additions and removals from the scene with QGraphicsItem::itemChange() however since you are the creator of your application you should be able to tell when you add or remove things from the scene. You can restrain yourself from calling QGraphicsScene::add*() methods in favour of exposing a custom scene API that will control when someone adds and removes items from the scene.