Issue using QDir/QDirIterator
Can someone tell me what is wrong with this code? It compiles fine, but I can’t seem to figure out how to set a directory.
- #include <QtCore/QDir>
- #include <QtCore/QDirIterator>
- #include <iostream>
- using namespace std;
- void someFunction()
- QString path = "C:/Users/";
- QDirIterator iter(path);
- if(iter.fileInfo().exists() || iter.fileInfo().isDir() || iter.fileInfo().isFile())
- cout << iter.filePath().toStdString();
- cout << "failed\n";
- int main()
- return 0;
It prints “failed” every time. What am I doing wrong?
The QDirIterator constructor takes a QDir or a directory as argument. After construction, the iterator is located before the first directory entry.
Your iterator is placed on an invalid entry. You will have to call QDirIterator::next() at least once to place it on the first valid entry.