如何删除QTreeWidgetItem [英] How to delete QTreeWidgetItem

查看:1570
本文介绍了如何删除QTreeWidgetItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个网页说QTreeWidgetItem可以通过删除或QTreeWidget.clear ing删除.但是我下面的代码示例似乎没有这样做.我做错什么了吗?

Several webpages say that QTreeWidgetItem can be deleted by deleting or QTreeWidget.clearing. But my code sample below doesn't seem to do so. Am I doing anything 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's 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()

打印为:

TreeWidgetItemChild init
Before clearing
After clearing
TreeWidgetItemChild del

在我看来,TreeWidgetItemChild是在流程终止时被删除的,而不是通过我的任何删除操作删除的.

Looks to me TreeWidgetItemChild gets deleted upon the termination of process, not by any of my deletion actions.

推荐答案

Python在内存管理/删除对象方面与C ++不同. Python有一个垃圾收集器(GC),可以自动管理对象的销毁.当对象的引用计数达到零时,会发生这种情况.

Python is different from C++ in the sense of memory management/deleting objects. Python has a garbage collector (GC) that manages destroying of the objects automatically. That occurs when the reference count of an object reaches zero.

del i仅表示将引用计数减一".它永远不会导致直接调用__del__.对象的__del__仅在引用计数达到零并即将被垃圾回收时调用. (尽管对于CPython而言确实如此,但并非每种实现都可以保证.它取决于GC的实现.因此,您完全不应该依赖__del__)

del i only means 'decrement the reference count by one'. It never results in a direct call to __del__. __del__ of an object is only called when reference count reaches to zero and is about to be garbage collected. (Although this is true for CPython, it's not guaranteed for every implementation. It depends on the GC implementation. So you should not rely on __del__ at all)

故事简短,__del__的通话时间不明确.您永远不要直接调用__del__(或任何其他__foo__特殊方法).实际上,由于上​​述原因,您应该完全避免使用__del__(通常).

Keeping story short, the call time of __del__ is ambiguous. You should never call __del__ (or any other __foo__ special methods) directly. In fact, for the reasons above you should rather avoid the use of __del__ at all (usually).

除此之外,还有另一个问题.

Apart from that, there is another issue.

tree.removeItemWidget(i, 0)

这不会从QTreeWidget中删除项目.顾名思义,它从项目中删除了 widget ,而不是QTreeWidgetItem.它与setItemWidget方法相对应,而不是addTopLevelItem方法.

This does not remove an item from QTreeWidget. As the name suggests, it removes a widget from an item, not the QTreeWidgetItem. It's counterpart to the setItemWidget method, not the addTopLevelItem method.

如果需要从树中删除特定项目,则应使用takeTopLevelItem.

If you need to remove a specific item from the tree, you should use takeTopLevelItem.

tree.takeTopLevelItem(tree.indexOfTopLevelItem(i))

tree.clear()可以.它将从树中删除所有顶级项.

tree.clear() is fine. It will remove every top level item from the tree.

这篇关于如何删除QTreeWidgetItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆