Results 1 to 13 of 13

Thread: How to delete items in QTreeWidget...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    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.

  2. #2
    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

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    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...

  4. #4
    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?

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    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 

  6. #6
    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...

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    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...

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