How to remove QTreeWidgetItem

Several web pages say that QTreeWidgetItem can be removed by deleting or QTreeWidget.clear ing. But my sample code below does not seem to do this. Am I doing something wrong?

 #!/usr/bin/python import sys from PySide.QtGui import QApplication, QWidget, QTreeWidget, QTreeWidgetItem #from PyQt4.QtGui import QApplication, QWidget, QTreeWidget, QTreeWidgetItem # Result was the same with `PySide` import time class TreeWidgetItemChild(QTreeWidgetItem): def __init__(self): super(TreeWidgetItemChild, self).__init__() print 'TreeWidgetItemChild init' def __del__(self): print 'TreeWidgetItemChild del' def test_QTree_clear_children(): tree = QTreeWidget() tree.setHeaderLabel('funksoul') i = TreeWidgetItemChild() tree.addTopLevelItem(i) print 'Before clearing' #tree.clear() # Didn't call destructor (__del__) #tree.removeItemWidget (i, 0) # Didn't call destructor #i.__del__() # Called destructor but it called again afterward del i # Didn't call destructor time.sleep(1) print 'After clearing' if __name__ == '__main__': app = QApplication(sys.argv) test_QTree_clear_children() 

Printed as:

 TreeWidgetItemChild init Before clearing After clearing TreeWidgetItemChild del 

It appears that TreeWidgetItemChild is TreeWidgetItemChild deleted after the process has completed, and not through any removal actions.

+6
source share
3 answers

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.

+6
source

By calling del i , you simply delete the link, not the actual C ++ object (referent) to which it refers, and not the object itself.

Change the TreeWidgetItemChild.__del__ to:

 def __del__(self): treeWidget = self.treeWidget() #removing the QTreeItemWidget object treeWidget.takeTopLevelItem(treeWidget.indexOfTopLevelItem(self)) print 'TreeWidgetItemChild del' 
+4
source

You enter empty tree elements (i.e. tree nodes) with widgets that this element can contain.

The following example creates a QTreeWidget and adds two elements to it: a top-level element and a nested element. By deleting comments, you can see how both of them are removed from the tree.

 #!/usr/bin/env python import sys from PyQt4.QtGui import * class MyMainWindow(QMainWindow): def __init__(self, parent=None): super(MyMainWindow, self).__init__(parent) self.tree = QTreeWidget(self) self.setCentralWidget(self.tree) self.tree.setHeaderLabel('funksoul') i = QTreeWidgetItem(self.tree, ['top level']) self.tree.addTopLevelItem(i) j = QTreeWidgetItem(i ,['nested level']) #i.takeChild(0) #self.tree.takeTopLevelItem(0) if __name__ == "__main__": app = QApplication(sys.argv) ui = MyMainWindow() ui.show() sys.exit(app.exec_()) 

To remove both types of items from the tree, you need an item index. If you have a link to the item you want to remove, you can get the corresponding index using the indexOfTopLevelItem and indexOfChild .

+3
source

All Articles