Friday, 17 July 2020

Removing rows from QTreeWidget (qt programming)

what's the best way to remove a row (QTreeWidgetItem) from a QTreeWidget?

The QTreeWidget content has been set by:

myQTreeWidget->insertTopLevelItems(0, items); // items = QList<QTreeWidgetItem*>

then I remove an item from my QList 'items' and I try to clear/reset the QTreeWidget

packList->clear();    
packList->insertTopLevelItems(0, items);

but my app crashes here! Suggestions?


Answers:


From what this documentation says, you should be able to do it with:

packList->takeTopLevelItem(index);

Which returns removes and returns the item at the supplied index.


Answers:


Your problem is that calling packList->clear() deletes the tree widget items contained by the tree. (See the documentation about QTreeWidget::clear(), which includes a note about the items being removed from the tree before deleting.) You'll either need to find a way to remove the items, or not maintain a list of them separately from the tree.

On a slightly-related note, if you are trying to keep track of other data along with the tree, I'd recommend you try to use the models paradigm. In non-trivial cases, it has usually been worth my while to convert to that technique, rather than using the widgets/items.


Answers:


No comments:

Post a Comment