Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue using QDir/QDirIterator
Forum Updated to NodeBB v4.3 + New Features

Issue using QDir/QDirIterator

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    prophetjohn
    wrote on last edited by
    #1

    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();
    else
        cout << "failed\n";
    

    }

    int main()
    {
    someFunction();

    getchar();
    return 0;
    

    }
    @

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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      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() << it.next();

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

      }
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved