简体   繁体   English

PyQT QTreeWidget 迭代

[英]PyQT QTreeWidget iterating

I have two columns in a QTreeWidget , one column represents a list of urls and the second represents results .我在QTreeWidget 中有两列,一列表示url 列表,第二列表示结果 I have loaded the list of urls in first column and now I want to iterate this list and during the iteration, change the text in the second column.我已经在第一列中加载了 url 列表,现在我想迭代这个列表,并在迭代期间更改第二列中的文本。 How to achieve this?如何实现这一目标?

You can call QTreeWidget.invisibleRootItem() to receive the root item, and then use the QTreeWidgetItem API to iterate through the items.您可以调用QTreeWidget.invisibleRootItem()来接收根项,然后使用QTreeWidgetItem API 遍历这些项。

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由以下内容填充:

self.treeWidget.setColumnCount(2) # two columns, url result
for i in range(10):
    self.treeWidget.insertTopLevelItem(i, QTreeWidgetItem(QStringList('url %s' % i)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM