How to delete QGraphicsItem properly?

qt

Solution

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

Problem

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?

Original source