Getting a QTreeWidgetItem List again from QTreeWidget

Since you're dealing with a tree, the API is designed to give you access to the QTreeWidgetItems in a tree-structure. Thus there is no direct way to simply get access to every single QTreeWidgetItem directly through Qt's API. There are, however, two ways you can do this:

1) If all of your items (or all the items you care about) are "top-level" then you can do something like this:

for( int i = 0; i < tree->topLevelItemCount(); ++i )
{
   QTreeWidgetItem *item = tree->topLevelItem( i );

   // Do something with item ...
}

2) If you need to access every item in the tree, along with that item's children, then a recursive approach may be in order:

doStuffWithEveryItemInMyTree( tree->invisibleRootItem() );

void doStuffWithEveryItemInMyTree( QTreeWidgetItem *item )
{
    // Do something with item ...

    for( int i = 0; i < item->childCount(); ++i )
        doStuffWithEveryItemInMyTree( item->child(i) );
}

If you want to get a list of all QTreeWidgetItem in a QTreeWidget you can do a

QList<QTreeWidgetItem *> items = ui->treeWidget->findItems(
            QString("*"), Qt::MatchWrap | Qt::MatchWildcard | Qt::MatchRecursive);

The code below is in Python, but it can be easily translated to C++. I had exactly the same problem as the one described in the question, but I was using PySide (Python Qt binding).

If you want to get a list of all QTreeWidgetItems under a given item (including that item itself), use the first function. To get a list of all QTreeWidgetItems in a tree, call the second function.

def get_subtree_nodes(tree_widget_item):
    """Returns all QTreeWidgetItems in the subtree rooted at the given node."""
    nodes = []
    nodes.append(tree_widget_item)
    for i in range(tree_widget_item.childCount()):
        nodes.extend(get_subtree_nodes(tree_widget_item.child(i)))
    return nodes

def get_all_items(tree_widget):
    """Returns all QTreeWidgetItems in the given QTreeWidget."""
    all_items = []
    for i in range(tree_widget.topLevelItemCount()):
        top_item = tree_widget.topLevelItem(i)
        all_items.extend(get_subtree_nodes(top_item))
    return all_items