6

I have created a QTreeWidget, I'm trying to list all the items displayed.

I do not want to go inside the items if the item have child but not expanded. It's really getting the number of Items I can see in the tree.

I have tried :

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

but this is giving me only the topLevelItem and I want all I can see. In the example, I should be able to count 14 items

enter image description here

Seb
  • 2,929
  • 4
  • 30
  • 73
  • I have to write a recursive function that will run over all items. – vahancho Mar 03 '15 at 10:20
  • Off-topic question: Is this a GNU/Linux OS with a custom theme or you were able to change the folder icons in the QTreeWidget? – Jacob Krieg Mar 03 '15 at 10:22
  • the icon are custom one :-). I have directly point to the icon I want by using setIcon – Seb Mar 03 '15 at 10:26
  • @vahancho There is no other way, a simple one ? – Seb Mar 03 '15 at 10:28
  • If you have a fixed (and small) number of levels you can do it in one function, otherwise you need the recursive function. But I'd say that already is a pretty simple way – Bowdzone Mar 03 '15 at 10:35

2 Answers2

6

You can write a recursive function that will run over the hierarchy and count all visible items. For example:

int treeCount(QTreeWidget *tree, QTreeWidgetItem *parent = 0)
{
    int count = 0;
    if (parent == 0) {
        int topCount = tree->topLevelItemCount();
        for (int i = 0; i < topCount; i++) {
            QTreeWidgetItem *item = tree->topLevelItem(i);
            if (item->isExpanded()) {
                count += treeCount(tree, item);
            }
        }
        count += topCount;
    } else {
        int childCount = parent->childCount();
        for (int i = 0; i < childCount; i++) {
            QTreeWidgetItem *item = parent->child(i);
            if (item->isExpanded()) {
                count += treeCount(tree, item);
            }
        }
        count += childCount;
    }
    return count;
}

And the usage:

QTreeWidget tw;
// Add items
[..]
int visibleItemsCount = treeCount(&tw);
vahancho
  • 20,808
  • 3
  • 47
  • 55
2

Just ran into this myself for PyQt. There's actually a much easier solution, you just need to use the QTreeWidgetItemIterator (which already loops over all items in the tree, as the name suggests). I don't know C++ so here's my python solution, however the theory is obviously the same. You want to iterate over the QTreeWidget and any items which are expanded should be counted. Namely:

def count_tems(self):
    count = 0
    iterator = QtGui.QTreeWidgetItemIterator(self) # pass your treewidget as arg
    while iterator.value():
       item = iterator.value()

        if item.parent():
            if item.parent().isExpanded():
                count +=1
        else:
            # root item
            count += 1
        iterator += 1
    return count
Spencer
  • 1,931
  • 1
  • 21
  • 44
  • 9
    *sigh* nothing like looking up a question and finding your own answer to the exact same thing 2 years ago... – Spencer Jul 10 '18 at 22:28