Reputation: 1367
What I should to do to delete QGraphicsItem?
To remove item from the scene I use
QGraphicsScene::removeItem(QGraphicsItem * item);
From the docs for this method:
i.e., QGraphicsScene will no longer delete item when destroyed
So I see only one way:
delete item;
But may be another? For example for QWidget is able to set attribute
setAttribute( Qt::WA_DeleteOnClose );
That causes to deleting of the object. May be there is something similar for QGraphicsItem?
Upvotes: 10
Views: 15024
Reputation: 21
It seems the issue lasting for long time today and there are open bugs also.
But it seems to have a workaround, which I could find it useful and after hours of debugging and reading and investigations I have found it here:
Some other tips and tricks regarding Graphics Scene here: https://tech-artists.org/t/qt-properly-removing-qgraphicitems/3063/6
Upvotes: 0
Reputation: 1605
Unfortunately, QGraphicsItem
is a base class of all graphics items usable inside QGraphicsScene
, and, as most "item-like" objects in Qt, is not derived from QWidget
or QObject
. Moreover, they can only be parented to another QGraphicsItem
(apart from being owned by QGraphicsScene
.
After removing an item from a scene, unless it is parented to another QGraphicsItem
, Qt expects the programmer to delete it manually by calling delete item;
explicitly (or to use a smart pointer to manage the lifetime of the item after it was removed).
Upvotes: 11