Results 1 to 11 of 11

Thread: How to clear all elements in a QGraphicsScene? (qt 4.2)

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13

    Question How to clear all elements in a QGraphicsScene? (qt 4.2)

    How do you do that?
    I get a lot of
    QGraphicsScene::removeItem: item 0x81bc010's scene ((nil)) is different from this scene (0x80f6388)

    when trying to

    Qt Code:
    1. if (myscene != NULL)
    2. {
    3. QList<QGraphicsItem*> L = myscene->items();
    4. while (!L.empty())
    5. {
    6. myscene->removeItem(L.first());
    7. delete L.first();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: How to clear all elements in a QGraphicsScene? (qt 4.2)

    Try:
    Qt Code:
    1. qDeletaAll( myscene->items() );
    To copy to clipboard, switch view to plain text mode 
    (you will have to add #include <QtAlgorithms>).

  3. The following user says thank you to jacek for this useful post:

    Morea (8th September 2006)

  4. #3
    Join Date
    Feb 2006
    Posts
    209
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13

    Default Re: How to clear all elements in a QGraphicsScene? (qt 4.2)

    Great, thanks. Qt feels quite large...

    What did I do wrong?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: How to clear all elements in a QGraphicsScene? (qt 4.2)

    Quote Originally Posted by Morea
    What did I do wrong?
    "delete L.first()" deletes only the object that the first item in the list points to --- it doesn't remove the item.

    Qt Code:
    1. while( ! L.empty() ) {
    2. myscene->removeItem( L.first() ); // this line isn't necessary --- item destructor will handle this
    3. delete L.first();
    4. L.removeFirst(); // <- the missing line
    5. }
    To copy to clipboard, switch view to plain text mode 
    qDeleteAll() is a shorthand for the above loop (without the removeItem() call).

  6. #5
    Join Date
    May 2007
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How to clear all elements in a QGraphicsScene? (qt 4.2)

    Quote Originally Posted by jacek View Post
    Qt Code:
    1. while( ! L.empty() ) {
    2. myscene->removeItem( L.first() ); // this line isn't necessary --- item destructor will handle this
    3. delete L.first();
    4. L.removeFirst(); // <- the missing line
    5. }
    To copy to clipboard, switch view to plain text mode 
    qDeleteAll() is a shorthand for the above loop (without the removeItem() call).
    This code however will have problems if some of the QGraphicsItems are children of other QGraphicsItems. As the children will automatically be destroyed with their parents, you'll get double frees and program crashes.

    I use the following code (which is a slot of my QGraphicsScene-derived class) to clear the scene:
    Qt Code:
    1. void MyGraphicsScene::clear()
    2. {
    3. bool itemDeleted = true;
    4. QList <QGraphicsItem*> itemList = items();
    5.  
    6. while (itemDeleted && !itemList.isEmpty()) {
    7. int numItems = itemList.size();
    8. int iItem;
    9. itemDeleted = false;
    10. for (iItem = 0; (iItem < numItems) && !itemDeleted; iItem++) {
    11. QGraphicsItem *item = itemList.at(iItem);
    12. if (item->parentItem() == NULL) {
    13. removeItem(item);
    14. delete item;
    15. itemDeleted = true;
    16. }
    17. }
    18. itemList = items();
    19. }
    20. if (!itemList.isEmpty() && !itemDeleted)
    21. qWarning("Problem in MyGraphicsScene::clear()");
    22. }
    To copy to clipboard, switch view to plain text mode 

    This doesn't look exactly pretty, but works for me.

  7. #6
    Join Date
    May 2007
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How to clear all elements in a QGraphicsScene? (qt 4.2)

    Quote Originally Posted by hb View Post
    This code however will have problems if some of the QGraphicsItems are children of other QGraphicsItems. As the children will automatically be destroyed with their parents, you'll get double frees and program crashes.
    Actually, thinking about it it's probably cleaner to first get a list of all top-level items, and then use the method jacek described to clear that list instead of the whole items() list.

    Code to get a list of all top-level items could look like this:
    Qt Code:
    1. QList<QGraphicsItem*> MyGraphicsScene::getTopLevelItems()
    2. {
    3. int numItems, iItem;
    4. QList<QGraphicsItem*> topLevel;
    5. QList<QGraphicsItem*> itemList = items();
    6.  
    7. numItems = itemList.size();
    8. for (iItem = 0; iItem < numItems; iItem++) {
    9. QGraphicsItem *item = itemList.at(iItem);
    10. if (item->parentItem() == NULL)
    11. topLevel.append(item);
    12. }
    13. return topLevel;
    14. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to clear all elements in a QGraphicsScene? (qt 4.2)

    Hm. QGraphicsScene::clear() would probably be nice.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  9. #8
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    37
    Thanked 53 Times in 40 Posts

    Default Re: How to clear all elements in a QGraphicsScene? (qt 4.2)

    Quote Originally Posted by Bitto View Post
    Hm. QGraphicsScene::clear() would probably be nice.
    True, also with an option to delete the cleared elements would be nice
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #9
    Join Date
    May 2007
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How to clear all elements in a QGraphicsScene? (qt 4.2)

    Quote Originally Posted by Bitto View Post
    Hm. QGraphicsScene::clear() would probably be nice.
    I agree. And personally, I would find a QGraphicsScene::topLevelItems() also useful, since I have the impression that in most applications, interaction is mostly done with toplevel items only.

  11. #10
    Join Date
    May 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to clear all elements in a QGraphicsScene? (qt 4.2)

    It's not exactly the most efficient way of doing things, but this seems to work pretty well for me.
    Qt Code:
    1. void SubClassedQGraphicsScene::clear(){
    2. QList <QGraphicsItem*> itemList = items();
    3. while(!itemList.isEmpty()){
    4. delete itemList.first();
    5. itemList = items();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    I think trolltech needs to put a clear() function in the QGraphicsScene class though.

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

    Default Re: How to clear all elements in a QGraphicsScene? (qt 4.2)

    Quote Originally Posted by klnusbaum View Post
    I think trolltech needs to put a clear() function in the QGraphicsScene class though.
    They did already, QGraphicsScene::clear() was introduced in Qt 4.4.
    J-P Nurmi

Similar Threads

  1. QTextBrowser Clear selection
    By sreedhar in forum Newbie
    Replies: 2
    Last Post: 4th April 2006, 08:23

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
  •  
Qt is a trademark of The Qt Company.