pythonpyqtqtreewidgetqtreewidgetitem

Get the most top-level item of a QTreeWidgetItem


I would like to find the most top-level parent item of a QTreeWidgetItem, no matter how many levels deep it is in the tree.

Problem with using item.parent() is that it could return another QTreeWidgetItem if the item is several levels deep in the tree, so I would end up having to do item.parent().parent().parent()...etc for example.


Solution

  • I didn't explain my question well. But while loop is indeed the solution, this will keep searching until it finds the top-level item of the item:

    while item.parent():
        item = item.parent()