user8028736
user8028736

Reputation:

How to iterate through all indexes in Tree Model

I want to iterate through all indexes in the Tree model as shown in the image.

The function that I have written gives stack overflow error.

void iterate(const QModelIndex & index, const QAbstractItemModel * model)
{
     if (index.isValid())
          PrintData( index );

     if (!model->hasChildren(index) || (index.flags() & Qt::ItemNeverHasChildren))
     {
          return;
     }
     auto rows = model->rowCount();
     for (int i = 0; i < rows; ++i)
         iterate(model->index(i, 0, index), model);
}

enter image description here

Upvotes: 7

Views: 2515

Answers (1)

Dimitry Ernot
Dimitry Ernot

Reputation: 6584

Pass the current index as parameter of QAbstractItemModel::rowCount() to get its number of rows. Otherwise, you will get the number of root items in your tree.

So, replace auto rows = model->rowCount(); by auto rows = model->rowCount(index);

Upvotes: 7

Related Questions