Results 1 to 13 of 13

Thread: How to delete items in QTreeWidget...

  1. #1
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to delete items in QTreeWidget...

    I'm using my classes & objects which inherit QTreeWidgetItem, and it crashes when I try to do remove an item in this way:

    Qt Code:
    1. ui->treeWidget->currentItem()->parent()->removeChild(ui->treeWidget->currentItem());
    To copy to clipboard, switch view to plain text mode 

    While this way succeeded:

    Qt Code:
    1. ui->treeWidget->invisibleRootItem()->removeChild(ui->treeWidget->currentItem());
    To copy to clipboard, switch view to plain text mode 

    But this can only remove top level items, and it still crashed when I try to remove the last one in treeWidget. What's more, in another project QTreeWidget::clear() made it crash, too... and I don't understand the documentation of this slot.

    So, why it crashed, and how to delete an item from a QTreeWidget? Thanx!

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to delete items in QTreeWidget...

    You remove it by deleting it, i.e. delete ui->treeWidget->currentItem().

    Your crashed is probably since the removeChild method deletes the current item, which you refer to when finding the parent. I.e. you return into a function which no longer has a valid context.

  3. #3
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to delete items in QTreeWidget...

    Er~~(╯﹏╰)b it seems I forgot the simplest way......

    But it still crashes when I try to delete all things in the treeWidget. Neither delete one by one nor QTreeWidget::clear() works

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to delete items in QTreeWidget...

    Debug your application and see where exactly your program crashes and post your back trace.

  5. #5
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to delete items in QTreeWidget...

    Er... I run with F5 and it tells me

    "The inferior stopped because it received a signal from the Operating System.
    Signal name: SIGSEGV
    Signal meaning: Segmentation fault"

    And... it must have crashed in QTreeWidgetItem::~QTreeWidgetItem(), but I don't know how to use debug to find out where it exactly is...

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to delete items in QTreeWidget...

    Assuming you are using Qt Creator. First build your application in debug mode, then start it via F5. Before closing the error dialog, show in the center bottom of creator, and there you will find the - graphical - back trace.

  7. #7
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to delete items in QTreeWidget...

    er... do u mean this which contains 43 lines? (in the attachment) I can understand nothing within...
    Attached Images Attached Images

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to delete items in QTreeWidget...

    as far as what I can see on your picture: in your slot on_treeWidget_it... you are accessing the text() function of an item, but you don't check the pointer to the item if it is not null. My guess. Show us the code of on_treeWidget_it...

  9. #9
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to delete items in QTreeWidget...

    Thanx a lot! I see what's wrong. There's a label in the project:

    Qt Code:
    1. void MainWindow::on_treeWidget_itemSelectionChanged()
    2. {
    3. ui->label->setText( ui->treeWidget->currentItem()->text(0) );
    4. }
    To copy to clipboard, switch view to plain text mode 

    it just show the name of which is selected. When nothing is available it crashes.

    Then I tried this and failed again:

    Qt Code:
    1. void MainWindow::on_treeWidget_itemSelectionChanged()
    2. {
    3. if(ui->treeWidget->topLevelItemCount()>0)
    4. ui->label->setText( ui->treeWidget->currentItem()->text(0) );
    5. }
    To copy to clipboard, switch view to plain text mode 

    I think when the treeWidget is empty all the members of it are unavailabe. But just how to keep it away from crashing if I still want to use this label?

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to delete items in QTreeWidget...

    That is not a valid check! You tree can have items but non must be selected! Use
    Qt Code:
    1. if (ui->treeWidget->currentItem())
    2. ui->label->setText( ui->treeWidget->currentItem()->text(0) );
    3. else
    4. ui->label->setText( "No item selected." );
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to delete items in QTreeWidget...

    Thanx! Finally it works!

    Not a tough problem relly, but has taken me 3 days...

  12. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to delete items in QTreeWidget...

    Quote Originally Posted by Patrick Sorcery View Post
    Not a tough problem relly, but has taken me 3 days...
    To avoid that and other common errors in future, I strongly suggest you to get familiar with the standard debugging and its output to realize what it is telling your. Just google for "gdb" (GNU debuger) and spend a half day of reading and you will save a lot of time later! And your state of health will also increase

    E.g. your problem could have been solved withing 3 minutes (or even faster) without looking at lines which don't cause the problem. And mainly without changing irrelevant code and make it worser...

  13. #13
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to delete items in QTreeWidget...

    Quote Originally Posted by e8johan View Post
    You remove it by deleting it, i.e. delete ui->treeWidget->currentItem().

    Your crashed is probably since the removeChild method deletes the current item, which you refer to when finding the parent. I.e. you return into a function which no longer has a valid context.
    Thanks... This post help me for my ListWidget.. Now the Remove Button works
    Thanks e8johan

Similar Threads

  1. removing items QTreeWidget
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2015, 21:58
  2. The order of items in QTreeWidget
    By KK in forum Qt Programming
    Replies: 4
    Last Post: 27th February 2009, 04:54
  3. Does clear()/clearContents() delete items?
    By vycke in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2008, 17:49
  4. Delete items retrieved from QTreeWidget.takeItem
    By bruccutler in forum Qt Programming
    Replies: 3
    Last Post: 22nd March 2007, 23:24
  5. delete items from list box
    By vvbkumar in forum Qt Programming
    Replies: 4
    Last Post: 23rd June 2006, 19:08

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.