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. QDir problem
QtWS25 Last Chance

QDir problem

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 3.8k Views
  • 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.
  • A Offline
    A Offline
    ahura_24
    wrote on last edited by
    #1

    hi guys . i have one problem
    @
    #include <QCoreApplication>
    #include <QtCore>
    #include <QVector>

    void search(QString fileName)
    {
    QDir dir( fileName );
    QVector<QFileInfo> v;

    foreach (QFileInfo file, dir.entryInfoList(QDir::NoDot | QDir::NoDotDot)) // doesnt work when i use filter mode
        v.push_back( file );
    
    for (QVector<QString>::iterator itr = v.begin(); itr != v.end(); ++itr)
        qDebug() << itr->absoluteFilePath();
    
    for (QVector<QString>::iterator itr = v.begin(); itr != v.end(); ++itr)
        if ( itr->isDir() ) search( itr->absoluteFilePath() + '/' );
    

    }

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    search( "D:/" );
    
    return a.exec&#40;&#41;;
    

    }
    @

    in drive D: i have some folder . when i use filter mode doesnt work ! i want use recursive function to iterate all file and folder in my drive D:

    tnx alot

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Rethink your design. In it's current state it is not only wrong, but also very suboptimal.

      In order for a method to be recursive, you have to have some object, where results of every iteration will be stored. This can be done in many ways, probably the most common is to make the recursive method (search in your case) return a list (in your case a QVector), that is appended to the previous one with every iteration.

      Also, in main, you need to get the result, otherwise the whole endeavour is pointless.

      I know this is not what you have asked for, by the way. Just my 2 cents to make your life easier :)

      (Z(:^

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        As for your problem, try using QDir::NoDotAndDotDot instead :)

        (Z(:^

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Also, take care not to follow symlinks. If you do, you might end up in an endless loop, which will result in a crash due to running out of stack space.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ahura_24
            wrote on last edited by
            #5

            i use QDir::NoDotAndDotDot but doesnt work ! when i use filter mode QDir::entryList return nothing !! but i dont know why ! when i dont use filter mode absolutepath return the address of parent directory -(for "." and "..") - and its make the stack overflow !

            1 Reply Last reply
            0
            • A Offline
              A Offline
              ahura_24
              wrote on last edited by
              #6

              how can i filter parent direcotory - . and .. - ? where my code is wrong ?

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                I'm sorry you're quite hard to understand. Will try to decode, but it might take time.

                @"where my code is wrong?" - I did give you some hints in my previous posts. Another one is this: you are iterating over every entry in "v" QVector, for every iteration of the foreach loop. This is very, very wrong (you are checking the same entry many times! And that can get you into memory problems very easily).

                I'm looking through QDir docs now, but cannot find any reason, why . and .. filters don't work for you. I remember I had some similar problem about a year ago, I'll try to find it and see what I did then. For now, you can do 2 things: manually exclude . and .. (just an if statement), and update your code according to my and Andre's hints, they will all make your life easier in the end :)

                (Z(:^

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ahura_24
                  wrote on last edited by
                  #8

                  ok tnx a lot . you say i iterator v and its wrong !! why ?

                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    In short and pseudocode:
                    @
                    foreach () {
                    an element is added to v;

                    for (go through ALL elements of 'v',
                    including those covered by previous
                    iteration of foreach):
                    }
                    @

                    This means that in iteration 1, the second "for" will go once, in iteration 2, it will loop 2 times (including over the first element, which was already covered by 1st iteration), in 3, it will loop 3 times, and so on. If you have some nested directories below the root, it will parse them multiple times. This is a serious problem.

                    (Z(:^

                    1 Reply Last reply
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

                      If you are not sure, how to create a recursive method, look it up with Google, there are plenty of examples all over the web. Getting it right is important, because recursive methods are very tricky, can lead to hard-to-debug errors and eat up loads of memory.

                      (Z(:^

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        ahura_24
                        wrote on last edited by
                        #11

                        ok tnx a lot

                        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