PyQT QTreeWidget iterating

前端 未结 1 1240
耶瑟儿~
耶瑟儿~ 2021-01-13 09:49

I have two columns in a QTreeWidget, one column represents a list of urls and the second represents results. I have loaded

相关标签:
1条回答
  • 2021-01-13 10:01

    You can call QTreeWidget.invisibleRootItem() to receive the root item, and then use the QTreeWidgetItem API to iterate through the items.

    Example:

    root = self.treeWidget.invisibleRootItem()
    child_count = root.childCount()
    for i in range(child_count):
        item = root.child(i)
        url = item.text(0) # text at first (0) column
        item.setText(1, 'result from %s' % url) # update result column (1)
    

    I am assuming self.treeWidget is populated by:

    self.treeWidget.setColumnCount(2) # two columns, url result
    for i in range(10):
        self.treeWidget.insertTopLevelItem(i, QTreeWidgetItem(QStringList('url %s' % i)))
    
    0 讨论(0)
提交回复
热议问题