首页 > 解决方案 > 获取 QTreeWidgetItem 的最顶层项

问题描述

我想找到 a 的最高级别父项QTreeWidgetItem,无论它在树中有多深。

使用的问题item.parent()是,如果该项目在树中的几级深处,它可能会返回另一个QTreeWidgetItem,所以我最终不得不这样做item.parent().parent().parent()...etc

标签: pythonpyqtqtreewidgetqtreewidgetitem

解决方案


我没有很好地解释我的问题。但是while循环确实是解决方案,这将继续搜索,直到找到该项目的顶级项目:

while item.parent():
    item = item.parent()

推荐阅读