Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete QGraphicsItem properly?

Tags:

qt

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?

like image 525
kaa Avatar asked Nov 28 '14 12:11

kaa


1 Answers

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).

like image 76
Martin Prazak Avatar answered Oct 16 '22 19:10

Martin Prazak