Reputation:
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);
}
Upvotes: 7
Views: 2515
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