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&#40;&#41;&#41;
        cout << iter.filePath().toStdString();
        cout << "failed\n";


    int main()

    return 0;


    It prints "failed" every time. What am I doing wrong?

  • bq. 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.
    QDirIterator it("/etc", QDirIterator::Subdirectories);
    while (it.hasNext()) {
    qDebug() <<;

    // /etc/.
    // /etc/..
    // /etc/X11
    // /etc/X11/fs
    // ...


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.