如何删除QTreeWidgetItem的子项。有时它会删除所有项目,有时则不会。
如何正确删除子项。
下面是我的函数,用于删除子项和添加新子项:
def update_children_cont(self,subDirs,parent):
new_dir = subDirs
for i in range(parent.childCount()):
parent.removeChild(parent.child(i))
print "child count should be 0",parent.childCount()
parent.addChildren(self.tree_list_generator(new_dir))
在 QWidgetItem.removeChild
的文档中:
删除指定的子项child。被删除的子项将不会被删除。
removed item will not be deleted
的意思是:被移除的子项不会被永久删除。
for i in range(parent.childCount): parent.removeChild(parent.child(0))
,因为下一个项目会向上移动,所以不需要反转。 - retnikt