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?
Asked
Active
Viewed 2.3k times
7
-
can you show us the code please? – aayoubi Jan 22 '12 at 14:27
-
1If you only have two columns, `QTreeWidget` could be replaced with `QTableWidget`. – reclosedev Jan 22 '12 at 14:38
1 Answers
15
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)))

ekhumoro
- 115,249
- 20
- 229
- 336

reclosedev
- 9,352
- 34
- 51
-
-
2This is effectively infeasible to discover merely by reading the official documentation. The traditional means of iterating tree items is via the [`QTreeWidgetItemIterator` helper class](https://riverbankcomputing.com/pipermail/pyqt/2014-May/034315.html) – which sadly provides no means of iterating *only* top-level tree items and hence is inapplicable in this common use case. Can we collectively admit that the Qt API is sufficiently obscure as to be useless without StackOverflow? (*Yes. Yes, we can.*) – Cecil Curry Oct 06 '18 at 02:00