PDA

View Full Version : Issue using QDir/QDirIterator



prophetjohn
14th September 2011, 04:45
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();
else
cout << "failed\n";
}

int main()
{
someFunction();

getchar();
return 0;
}
It prints “failed” every time. What am I doing wrong?

norobro
14th September 2011, 05:33
From the QDirIterator docs:
After construction, the iterator is located before the first directory entry.

prophetjohn
14th September 2011, 05:51
Thanks. While testing I found that iter.hasNext() evaluated to true, so i tried iter.next() before the if/else block and it still prints "failed". Any ideas?

norobro
14th September 2011, 05:58
Works fine here:
QString path = "/var/spool/";
QDirIterator iter(path);
iter.next();
if(iter.fileInfo().exists() || iter.fileInfo().isDir() || iter.fileInfo().isFile())
qDebug() << iter.filePath();
else
qDebug() << "failed\n";
Outputs: "/var/spool/cron"

prophetjohn
14th September 2011, 06:04
Ah, I found my problem. Thanks!