Results 1 to 5 of 5

Thread: [SOLVED] Deleting an item from QTreeWidget

  1. #1
    Join Date
    Dec 2014
    Location
    Balti, Republic of Moldova
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default [SOLVED] Deleting an item from QTreeWidget

    Well, I'm confused a little: I'm trying to delete selected item from QTreeWidget (in my case it have multiple columns) and always my app crashes on this action. Sorry for asking this question again and again, but I realy can't understand where my mistake is. Here are couple of solutions I've tryed (please, forgive me my english):

    Qt Code:
    1. QList<QTreeWidgetItem *> items = ui->treeWidget->selectedItems();
    2. if (!items.isEmpty()) {
    3. foreach (QTreeWidgetItem *item, items)
    4. delete item;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QTreeWidgetItem *twi = ui->treeWidget->currentItem();
    2. delete twi;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QTreeWidgetItem *parent = ui->treeWidget->currentItem()->parent();
    2. int index;
    3.  
    4. if (parent) {
    5. index = parent->indexOfChild(ui->treeWidget->currentItem());
    6. delete parent->takeChild(index);
    7. } else {
    8. index = ui->treeWidget->indexOfTopLevelItem(ui->treeWidget->currentItem());
    9. delete ui->treeWidget->takeTopLevelItem(index);
    10. }
    To copy to clipboard, switch view to plain text mode 

    I've tryed to set focus on another item before call "delete" but with no result... Please, any help. I started to code with Qt/C++ a couple of weeks ago and many things looks difficult to me.
    Last edited by xsid; 29th December 2014 at 21:15.

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Deleting an item from QTreeWidget

    Do not delete tree items this way. A tree is a linked structure so that a direct delete causes an invalid pointer in the tree. A crash will follow soon. You need to remove the item from the tree first and then delete it. QTreeWidgetItem has the following methods:

    removeChild( QTreeWidgetItem *child ) - Removes a child of this item from the tree structure but it does not delete it.
    parent() - returns a parent of this item.

    Therefore:
    Qt Code:
    1. QList<QTreeWidgetItem *> items = ui->treeWidget->selectedItems();
    2. QTreeWidgetItem *pp = nullptr;
    3.  
    4. if ( !items.isEmpty() )
    5. {
    6. foreach (QTreeWidgetItem *item, items)
    7. {
    8. pp = item->parent();
    9. pp->removeChild(item);
    10. delete item;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    and so on. Note that every "real" item has a parent, at least, the invisible root item. Note also, that the removed item can have childs. removeChild() will remove the whole branch and delete will delete all items in the branch recursively.

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

    xsid (29th December 2014)

  4. #3
    Join Date
    Dec 2014
    Location
    Balti, Republic of Moldova
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Deleting an item from QTreeWidget

    No, still crash with message "ASSERT failure in QList<T>::at: "index out of range"". What am I doing wrong? Crash appears even I'm trying to clear treeWidget and load data in it again, not just on item deleting.

  5. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Deleting an item from QTreeWidget

    Then the problem is with foreach. Most likely, as you delete the list items, the indexing of items (crashing is QTreeWidget::at()) becomes invalid. I suggest while and using QList::takeFirst() as a more sure processing the list. takeFirst() removes the first item from the list and returns it (or it returns nullptr if the list is empty).

  6. The following user says thank you to Radek for this useful post:

    xsid (29th December 2014)

  7. #5
    Join Date
    Dec 2014
    Location
    Balti, Republic of Moldova
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Deleting an item from QTreeWidget

    The problem was not in code. Every solutions above works. In the widget properties MUST be set selectionBehavior->SelectRows (I guess in case of multiple columns such mine case). Thank you, Radek, your code is very usefull and thanks for clarification about how to delete TreeWidget item correctly!
    Last edited by xsid; 29th December 2014 at 21:18.

Similar Threads

  1. Replies: 0
    Last Post: 21st October 2013, 09:16
  2. Problem while deleting item in QTreeView
    By QtNewbieNeedsHelp in forum Newbie
    Replies: 1
    Last Post: 4th September 2013, 06:46
  3. Replies: 0
    Last Post: 1st May 2012, 23:11
  4. Removing item from layout without deleting it.
    By mrc_pl in forum Qt Programming
    Replies: 2
    Last Post: 24th January 2012, 11:24
  5. deleting first item in a list
    By soul_rebel in forum Qt Programming
    Replies: 2
    Last Post: 6th April 2006, 17:49

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.