How can I remove a QGraphicsItem that has children?
Create graphics for this element as follows:
class MyClass : public QGraphicsItem
{
...
private:
SomeKindOfGraphicsItem* _item1;
SomeOtherGraphicsItem* _item2;
}
MyClass::MyClass( ... ) :
QGraphicsItem( ... ),
_item1( new SomeKindOfGraphicsItem( ..., this ) ),
_item2( new SomeOtherGraphicsItem( ..., this ) )
{
...
}
In this case, it is sufficient to remove only the parent element from the scene ( MyClass
in this example). This will remove all children as well:
void QGraphicsScene :: removeItem (QGraphicsItem * item)
Removes the element and all of its children from the scene. Ownership of the item is transferred to the caller (i.e. QGraphicsScene will no longer remove the item when it is destroyed).
Also, when an object MyClass
is removed, it removes all of its children using mechanics Qt
:
QGraphicsItem :: ~ QGraphicsItem () [virtual]
Destroys the QGraphicsItem and all of its children. If this item is currently associated with a scene, the item will be removed from the scene until it is removed.
Note. Effectively remove the item from the QGraphicsScene before destroying the item.
source to share