Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 602
Giancarlo Avatar asked Sep 15 '26 18:09

Giancarlo


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

like image 127
Caleb Huitt - cjhuitt Avatar answered Sep 19 '26 06:09

Caleb Huitt - cjhuitt


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.

like image 27
Soviut Avatar answered Sep 19 '26 04:09

Soviut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!