Python differs from C ++ in terms of memory management / object deletion. Python has a garbage collector (GC) that automatically controls the destruction of objects. This happens when the object's reference count reaches zero.
del i means only βreducing the number of links per unitβ. This never results in a direct call to __del__ . __del__ object is only called when the reference count reaches zero and is about to collect garbage. (Although this is true for CPython, it is not guaranteed for every implementation. It depends on the GC implementation. So you should not rely on __del__ at all)
In short, the __del__ call __del__ ambiguous. You should never call __del__ (or any other __foo__ special methods) directly. In fact, for the above reasons, you should avoid using __del__ at all (usually).
In addition, there is another problem.
tree.removeItemWidget(i, 0)
This does not remove the item from the QTreeWidget . As the name suggests, it removes the widget from the element, not QTreeWidgetItem . It is similar to the setItemWidget method, not the addTopLevelItem method.
If you need to remove a specific item from the tree, you should use takeTopLevelItem .
tree.takeTopLevelItem(tree.indexOfTopLevelItem(i))
tree.clear() great. It will remove each top level item from the tree.
source share