Results 1 to 8 of 8

Thread: Clear a Scene

  1. #1
    Join Date
    Mar 2008
    Location
    Morocco
    Posts
    47
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Clear a Scene

    Hi..
    I have a problem to clear a Scene !!
    I used the first methode :

    Qt Code:
    1. void myview::clearScene()
    2. {
    3. QList<QGraphicsItem*> itemsList = scene->items();
    4. QList<QGraphicsItem*>::iterator iter = itemsList.begin();
    5. QList<QGraphicsItem*>::iterator end = itemsList.end();
    6. while(iter != end)
    7. {
    8. QGraphicsItem* item = (*iter);
    9. delete item;
    10. iter++;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    but it doesnt work !!
    then I used another methode:
    Qt Code:
    1. void myview::clear(){
    2. QList<QGraphicsItem*> itemsList = scene->items();
    3. QGraphicsItemGroup *group=scene->createItemGroup(itemsList);
    4. scene->destroyItemGroup(group);
    5. }
    To copy to clipboard, switch view to plain text mode 
    this that One stucks the application !!

    if someone could help !!
    thanks
    Last edited by jpn; 13th May 2008 at 08:25. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Clear a Scene

    You could iterate the items in the scene and call QGraphicsScene::removeItem .
    Then you can delete the actual item.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Clear a Scene

    What do you mean by "doesn't work"? Please, be more exact next time. A QGraphicsItem removes itself from the scene once it gets deleted (as mentioned in destructor docs) so you can just do:
    Qt Code:
    1. qDeleteAll(scene->items());
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. #4
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Clear a Scene

    That's not a good idea; when you delete an item, it will delete all its children, and that list will surely contain pointers to those children as well / since the list is a copy, those pointers will remain after the children are dead. When the loop continues to those (now stale) pointers, the application will surely crash.

    Because of this, you must ensure that you only delete toplevel items. Checking if each item in the list returned by items()' parents are null isn't enough; the item may already be dead by the time you dereference the pointer. The items must be copied away first. For example:

    Qt Code:
    1. QList<QGraphicsItem *> topLevels;
    2. foreach (QGraphicsItem *item, scene->items()) {
    3. if (!item->parentItem())
    4. topLevels << item;
    5. }
    6. qDeleteAll(topLevels);
    To copy to clipboard, switch view to plain text mode 

    In Qt 4.4, you can call scene->clear().

    http://doc.trolltech.com/4.4/qgraphicsscene.html#clear
    Last edited by Bitto; 14th May 2008 at 21:37. Reason: Bug in code
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Clear a Scene

    As far as I remember items are placed in this list in such a manner than children are later in the list than their parents, so you could start deleting items starting from the end of the list and be safe. I'm not sure about the order though, so I might be totally wrong. And even if I'm not, the order might change in future releases of Qt, so it's safer to stick with copying top-levels to a separate list. This post is just to suggest a could-be solution and bump my forum post count

    BTW. Nice to have you back, Andreas.

  6. #6
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Clear a Scene

    Actually the plain items() function returns items in no particular order. But in any case even if it did, the returned list would still contain stale pointers to the deleted items :-).
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Clear a Scene

    Quote Originally Posted by Bitto View Post
    But in any case even if it did, the returned list would still contain stale pointers to the deleted items :-).
    If it did keep that order, you'd be deleting children before their parents, so no extra deletions would have taken place, because while deleting an item, its children would already have been deleted. But since items() doesn't keep that order (maybe it was some other list that kept that order - like qFindChildren() or something) this is all academic talk

  8. #8
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Cool Re: Clear a Scene

    Right you are, haha, if you sort the items by stacking order, the first items in the list will be the topmost ones, which happen to be the deepest descendents. Iterating this list is essentially list a topological sort, so qDeleteAll on this list is safe.

    Gah, I didn't think about that! ;-)))
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. Creating new scene from a part of an old one
    By maverick_pol in forum Qt Programming
    Replies: 6
    Last Post: 28th November 2007, 18:14
  3. How to update scene after removing items
    By nileshsince1980 in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2007, 09:56
  4. Creating a scene from piece of another scene
    By maverick_pol in forum Qt Programming
    Replies: 3
    Last Post: 23rd August 2007, 17:51
  5. How to use QGraphicsView and Scene right ...
    By Mike in forum Qt Programming
    Replies: 6
    Last Post: 22nd January 2007, 08:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.