3

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?

braX
  • 11,506
  • 5
  • 20
  • 33
JuanDeLosMuertos
  • 4,532
  • 15
  • 55
  • 87

2 Answers2

3

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.

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
1

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.

Soviut
  • 88,194
  • 49
  • 192
  • 260