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
You can write a recursive function that will run over the hierarchy and count all visible items. For example:
And the usage:
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: