Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top