Search code examples
pythonqtpyqtqtreewidget

Adding a row to QTreeWidget


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.


Solution

  • 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.