Delete entire QTreeWidget tree

时间:2015-07-31 19:52:38

标签: qt user-interface

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 个答案:

答案 0 :(得分:0)

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