1

I am trying to iterate over a part of a QTreeWidget. So I use a QTreeWidgetItemIterator and the constructor with a QTreeWidgetItem. But the iterator does not visit only the item and its children but "ascend" and continue after the given item.

QTreeWidgetItem *root = topLevelItem(0);  
QTreeWidgetItemIterator it(root);

while (*it)
{
   qDebug() << (*it)->text(0);
   ++it;
}

So I obtain this instead of having only 1x nodes.

node1
    node11
    node12
node2
    node21

Is it normal ? Is it possible to use this iterator to iterate on a node ?

Thanks.

Maluna34
  • 245
  • 1
  • 16

1 Answers1

2

Not very familiar with QTreeWidget, but I rerun something similar, and it seems that their iterator iterates from the starting node to the end of the tree. In this case this should do the job (although a little bit ugly):

QTreeWidgetItem *root = topLevelItem(0);
QTreeWidgetItemIterator it(root);
int nodesCount = 1; //for root, each visited child will update the count

for (int i = 0; i < nodesCount; ++i)
{
   qDebug() << (*it)->text(0);
   nodesCount += (*it)->childCount; //update the count for this node children
   ++it;
}
Anorflame
  • 376
  • 3
  • 12
  • Thanks for your help. But this iterate only over the first children of root and is not recursive ? – Maluna34 Sep 19 '15 at 14:11
  • Doesn't quite help there are several better ways to iterate over a qtreewidgetItem but this one is like not using iterators at all .I reached this questions because I made a function that takes a qtreewidgetiterator and now I want to use it with a "subtree". – Spyros Mourelatos Nov 24 '20 at 07:23