0
votes

Im working in a GUI project that is using QTreeWidget. The program parses through an XML and creates a tree to be displayed using the QTreeWidget.

I found a bug in the code, where if the user changes to a new selection, then back to the one they were just on, it re-parses the XML as it should, but the previous tree that was created is not being destroyed first.

The new tree is simply appended onto the old tree and then it is now displaying duplicates of everything. If you continue to change views and back again it will keep appending onto the tree n number of times.

I have tried using TreeWidget->clear(), but this looks like it only clears what is being displayed and is not actually deleting the tree itself.

I have seen examples on line of people talking about using delete topLevelItem() but i have not been able to make this work either.

Is there anyone out there who knows a proper way to delete and entire tree from a QTreeWidget?

Thanks.

1
"QObjects organize themselves in object trees. When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is." (qt.io). Is this way convenient for you ?Martin
@Martin: Just that QTreeWidgetItems aren’t QObjects, so your quotation doesn’t apply.Frank Osterfeld
I mean, I wanted to highlight the parents/child mechanism Qt use to delete objects and if you delete your QTreeWidget, all the sub-objects are also deleted. Anyway, you found your solution on your own :)Martin

1 Answers

0
votes

After an hour or so of searching through the code i found the problem. The original developer was storing the parsed XML inside a QStringList and the list was never getting cleared.

It was a simple as putting a list.clear() at the beginning of the function call...