PyQT QTreeWidget iterating

懵懂的女人 提交于 2019-12-19 09:56:38

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/8961449/pyqt-qtreewidget-iterating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!