Results 1 to 7 of 7

Thread: Removing items properly from qtreewidget item

  1. #1
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Removing items properly from qtreewidget item

    I am using Qt 4.1, and my application works with qtreewidget. It crashes when I am destroying items from tree ( executing this code):

    void Search:eleteItem(QTreeWidgetItem *item)
    {
    if (!item) return;

    for(int i=item->childCount(); i>0; i--) {
    DeleteItem(item->child(i));
    }
    delete item;
    }

    This piece of code is meant to delete all items from qtreewidget. I am calling this method number of times equal to number of top level items. Any advice? Is there any better way to delete qtreewidgetitems from qtreewidget?

    Any idea's are apreciated, I am complete newbie with Qt....

  2. #2
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Removing items properly from qtreewidget item

    The item's children are numbered 0 .. childCount()-1 , not 1 .. childCount()

    Also, QT Objects will automatically kill their children when they die.
    Last edited by drhex; 20th November 2006 at 23:10. Reason: updated contents

  3. #3
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Removing items properly from qtreewidget item

    So, if I want to delete all items in qtreewidget, I could just delete top level items? Funny, though, my application is actualy crashing when I am doing delete of my top level items. I have changed my code, and I have found out that this is the issue. What could go wrong when tring to delete top level items of qtreewidget?

    Thanx for your reply

  4. #4
    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: Removing items properly from qtreewidget item

    QTreeWidget cleans up it's content upon destruction. All items are deleted when the QTreeWidget is deleted. If you want to explicitly cleanup the tree, you can simply call QTreeWidget::clear(). The crash was caused by an incorrect index, like drhex already mentioned.
    J-P Nurmi

  5. #5
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Removing items properly from qtreewidget item

    Yes, I just want to explicitly delete just all items except the top level ones. I am trying numerous ways and the thing is whenever I try to delete qtreewidgetitem application crashes. So, do you know any method which deletes all items and leaves top level ones? That would solve my problem.

  6. #6
    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: Removing items properly from qtreewidget item

    Qt Code:
    1. for (int i = 0; i < topLevelItemCount(); ++i)
    2. {
    3. qDeleteAll(topLevelItem(i)->takeChildren());
    4. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    milli (20th April 2011)

  8. #7
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Removing items properly from qtreewidget item

    Thanx man! That solve problem. You rock!

Similar Threads

  1. Replies: 1
    Last Post: 21st September 2006, 11:37
  2. Get a certain item from a QTreeWidget
    By mace in forum Newbie
    Replies: 2
    Last Post: 28th August 2006, 11:35
  3. insert Item to my QTreeWidget
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 28th February 2006, 16:42

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.