[SOLVED] Remove an item from the child items list of QGraphicsItem



  • Greetings.

    How I can remove an item from the child items list of QGraphicsItem?.

    So far I have considered using the following:

    @
    QGraphicsItem::childItems()
    QGraphicsItem::setParentItem(QGraphicsItem*)
    QList::removeAt(int i) /* or a similar function for remove a item of a list */
    @

    I do not know in what order to invoke the last two functions.
    Nor if there is a cleaner, efficient and safe way to do what I want so.

    Thanks in advance for any help and/or suggestions.


  • Lifetime Qt Champion

    Hi,

    AFAIK, setting it's parent to something else should remove it from the list so your wouldn't need that last line.



  • After establishing the new parent of the child item, eg using

    @myItem->setParentItem(0)@

    ... I can free the memory of recently removed child item?



  • If you want to delete child item you do not need to remove it,
    just delete it.

    delete item;



  • When I say "remove" I mean establishing a new parent item for the child item; with @QGraphicsItem::setParentItem()@

    Now, when you say:

    bq. If you want to delete child item you do not need to remove it,
    just delete it.

    You mean that's not necessary and that I can use the operator 'delete' on chil item (without invoking 'QGraphicsItem::setParentItem()' before)?



  • yes, deleting item will delete all children and remove it from the scene



  • bq. yes, deleting item will delete all children and remove it from the scene

    My uncertainty is about the status of the parent item of the item removed.

    Will doing 'delete' an item from the list of child items, in the way you suggest me, not give rise to any inconsistency?



  • The deleted GraphicsItem takes care of that in its destructor.

    Edit: However, should you have a copy of the childItems list, then you can potentially use an invalid pointer. Naturally.



  • Well, all doubts were clarified.

    Thanks you all!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.