Adding a row to QTreeWidget


Question

How to add a row to the QTreeWidget in PyQt? I've got some serious trouble with that - I have a list of QStrings and I don't know how to add it to the QTreeWidget, which is in another class (class Ui_Form) and the QStringList is in another one.

1
1
5/27/2013 9:26:18 AM

Accepted Answer

First you have to make a list of QTreeWidgetItem's and then add them ton the QTreeWidget. Example:

tree = your_qtreewidget  # replace every 'tree' with your QTreeWidget
strings = list_of_strings
l = []  # list of QTreeWidgetItem to add
for i in strings:
    l.append(QTreeWidgetItem(i))  # create QTreeWidgetItem's and append them
tree.addTopLevelItems(l)  # add everything to the tree

References: QTreeWidget and QTreeWidgetItem in the PyQt docs.

3
5/27/2013 11:48:09 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon