PyQT QTreeWidget 迭代 [英] PyQT QTreeWidget iterating

查看:62
本文介绍了PyQT QTreeWidget 迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 QTreeWidget 中有两列,一列表示网址列表,第二列表示结果.我已经在第一列中加载了 url 列表,现在我想迭代这个列表,并在迭代期间更改第二列中的文本.如何实现这一目标?

I have two columns in a QTreeWidget, one column represents a list of urls and the second represents results. 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. How to achieve this?

推荐答案

您可以拨打 QTreeWidget.invisibleRootItem() 接收根项,然后使用QTreeWidgetItem 用于遍历项目的 API.

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

示例:

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)

我假设 self.treeWidget 由以下内容填充:

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)))

这篇关于PyQT QTreeWidget 迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆