关于pyqt treewidget中underlying C/C++ object has been deleted的错误

   在pyqt中使用treewidget,然后想清空此widget,为了简便,使用了clear()  槽,但是后来再次添加item的时候报错:
--------------------------------
underlying C/C++ object has been deleted
--------------------------------
qt文档中clear()说明:
Since each item is removed from the tree widget before being deleted, the return value ofQTreeWidgetItem::treeWidget() will be invalid when called from an item's destructor.
意思是说,调用此槽函数的效果是:是在删除(delete) item之前将其从TreeWidget中移除(removed),因此从item的析构函数中调用QTreeWidgetItem::treeWidget()的返回值(此时为空)是无效的,所以报错说“已经被删除了(has been deleted)”。因此,如果想调用clear()删除所有item,就必须先将所有item删除(delete),然后再clear(),或者先获得topLevelItemCount(),然后再takeTopLevelItem(int index)。


blog.163.com/yungang_z/blog/static/1751531332011228528195/


你可能感兴趣的:(return,BEFORE,Invalid,widget,called)