[Qt-interest] remove QgRaphicsItem from parent

Rainer Wiesenfarth Rainer.Wiesenfarth at inpho.de
Mon Oct 12 14:10:01 CEST 2009


From: Josiah Bryan [mailto:jbryan at productiveconcepts.com] 
> I probably would do:
> 
>   while (! elements.isEmpty ())
>   {
>   	pointer = elements.takeFirst ();
>         pointer->setParent(0);
>         myGraphicsScene->removeItem(pointer);
>         delete pointer;
>   }
> 
> Docs say setParent(0) sets removes the item from it's current 
> parent and 
> makes it a child of the QGraphicsScene (e.g. no parent.) Then we can 
> call removeItem() on the graphics scene to get it removed. 
> Finally, we 
> can delete the pointer once we're done with it.

Docs say also:

  QGraphicsItem::~QGraphicsItem () [virtual]

  Destroys the QGraphicsItem and all its children. If this item is
  currently associated with a scene, the item will be removed from
  the scene before it is deleted.

So removeItem() is not needed, as is probably the setParent(). Thus my
initial suggestion:

  while (! elements.isEmpty ())
  {
      delete elements.takeFirst ();
  }

This is as minimal as I would expect it to be. At least, since the
setAutoDelete() was dropped ... ;-)

Best Regards / Mit freundlichen Grüßen
Rainer Wiesenfarth

-- 
INPHO GmbH * Smaragdweg 1 * 70174 Stuttgart * Germany
phone: +49 711 2288 10 * fax: +49 711 2288 111 * web: www.inpho.de
place of business: Stuttgart * managing director: Johannes Saile
commercial register: Stuttgart, HRB 9586
Leader in Photogrammetry and Digital Surface Modelling
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3438 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091012/2de2b9a5/attachment.bin 


More information about the Qt-interest-old mailing list