【问题标题】:Get the most top-level item of a QTreeWidgetItem获取 QTreeWidgetItem 的最顶层项
【发布时间】:2020-03-22 21:36:30
【问题描述】:

我想找到QTreeWidgetItem 的最顶层父项,无论它在树中有多深。

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

【问题讨论】:

  • 你听说过 while() 循环吗?
  • 你不是在找QTreeWidget::invisibleRootItem吗?
  • 或者...您是否正在尝试获取同样是感兴趣的QTreeWidgetItem 的祖先的不可见根项的子项?如果是这样,您只需要实现一个简单的迭代或递归算法,搜索根并在适当的点停止。

标签: python pyqt qtreewidget qtreewidgetitem


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 2012-02-16
    • 2021-11-13
    • 1970-01-01
    • 2011-01-03
    • 2015-04-25
    相关资源
    最近更新 更多