1

How can I add a list of directories and subdirectories to a QStringList?

This is what I have...

QStringList dirList;

QDirIterator iterateFolders("/Users/userName/targetFolder", QDir::Dirs, QDirIterator::Subdirectories);
while (iterateFolders.hasNext())
{
    dirList.append(iterateFolders.next());
}

But I don't think it is working correctly because when I iterate though the list it doesn't show all of the folders, it skips some of them.

for(int i=0; i<dirList.length(); i++)
{
    qDebug() <<" Dir At: " << dirList.at(i);
}

What is the correct way to add directories and subdirectories to a QStringList?

Thanks

László Papp
  • 51,870
  • 39
  • 111
  • 135
fs_tigre
  • 10,650
  • 13
  • 73
  • 146
  • 2
    This code should work fine. Are you sure that you're iterating the path you want? Add qDebug() << iterateFolders.filePath(); after append – Kamil Klimek Dec 24 '13 at 08:27
  • Got it, after adding what you suggested I realized that my problem was that I had the while and the for loop in the same function, I moved the for loop to a different function and problem solved, it now shows all of the files. Thanks a lot. – fs_tigre Dec 24 '13 at 12:51
  • 2
    It should not be problem if they're in same function. You probably did something else wrong but you gave not enough code to reproduce problem – Kamil Klimek Dec 24 '13 at 13:32

1 Answers1

1

What is the correct way to add directories and subdirectories to a QStringList?

What you wrote is correct.

As it seems you have now fixed the issue, we can only say that the error was in a different place of your code, but not with for and while being in the same function or method per se. If they are sequential, i.e. the for loop followed the while loop, it should been fine since by the point of printing, you accumulated all the paths you are interested in.

László Papp
  • 51,870
  • 39
  • 111
  • 135