Results 1 to 5 of 5

Thread: Issue using QDir/QDirIterator

  1. #1
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt3
    Platforms
    Windows

    Default 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.

    Qt Code:
    1. #include <QtCore/QDir>
    2. #include <QtCore/QDirIterator>
    3. #include <iostream>
    4.  
    5. using namespace std;
    6.  
    7. void someFunction()
    8. {
    9. QString path = "C:/Users/";
    10. QDirIterator iter(path);
    11.  
    12. if(iter.fileInfo().exists() || iter.fileInfo().isDir() || iter.fileInfo().isFile())
    13. cout << iter.filePath().toStdString();
    14. else
    15. cout << "failed\n";
    16. }
    17.  
    18. int main()
    19. {
    20. someFunction();
    21.  
    22. getchar();
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 
    It prints “failed” every time. What am I doing wrong?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Issue using QDir/QDirIterator

    From the QDirIterator docs:
    After construction, the iterator is located before the first directory entry.
    Last edited by norobro; 14th September 2011 at 04:41. Reason: fixed link

  3. #3
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Issue using QDir/QDirIterator

    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?

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Issue using QDir/QDirIterator

    Works fine here:
    Qt Code:
    1. QString path = "/var/spool/";
    2. QDirIterator iter(path);
    3. iter.next();
    4. if(iter.fileInfo().exists() || iter.fileInfo().isDir() || iter.fileInfo().isFile())
    5. qDebug() << iter.filePath();
    6. else
    7. qDebug() << "failed\n";
    To copy to clipboard, switch view to plain text mode 
    Outputs: "/var/spool/cron"

  5. #5
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Issue using QDir/QDirIterator

    Ah, I found my problem. Thanks!

Similar Threads

  1. Replies: 1
    Last Post: 12th September 2011, 07:18
  2. QListView and QDir
    By Yayati.Ekbote in forum Qt Programming
    Replies: 5
    Last Post: 26th March 2010, 11:45
  3. Iterating with QDirIterator seems slow
    By TheShow in forum Qt Programming
    Replies: 1
    Last Post: 6th January 2010, 16:55
  4. QDirIterator and windows drives
    By invictus in forum Qt Programming
    Replies: 0
    Last Post: 8th March 2009, 19:48
  5. Is it me or QDir?
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 23:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.