Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QDirIterator have any hang issue, while iterate directories that have more sub directories.

QDirIterator have any hang issue, while iterate directories that have more sub directories.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 4 Posters 2.6k Views 3 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.
  • V Offline
    V Offline
    Vellayil
    wrote on last edited by
    #1

    Please see the below code for search *.pdf file from the directory, that have more sub directories (more than 2000 sub directory).

    QDirIterator it(dir, QStringList() << "*.pdf", QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext())
    {
    qDebug() <<"PDF file : " << it.next();
    }

    This code have any hang issue like recursion hang issue ?.

    Please share your thoughts.

    Thanks for your support in advance.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi,

      What do you mean by hang issue ?

      From your description you are at least exploring folders 2000 level deep whith who knows how many files in them so it might indeed take some times before the complete iteration is done.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply
      0
      • P Offline
        P Offline
        patrik08
        wrote on last edited by
        #3

        Hi....
        Here a sample:
        from https://github.com/pehohlva/AnalizeDir/blob/master/robotsystem.cpp

        void Robotsystem::followOneDir( const QString &namepathfull ) {
          QTextStream out(stdout);
          QDirIterator dirIt(namepathfull,QDirIterator::Subdirectories);
          while (dirIt.hasNext()) {
                    dirIt.next();
                    if (!dirIt.filePath().isNull()) {
                        Dirscount++;
                        QString spaceholder;
                        spaceholder.fill(QChar('.'),120);
                        QString fdirx = dirIt.filePath();
                        QStringList piecesdir = fdirx.split( "/" );
                        QString DirIndex= QString("Read %1:").arg(Dirscount);
                        if ( piecesdir.size() > 4 ) {
                          QString linewritteln = DirIndex + QString(" /") + piecesdir.at(3) + spaceholder;
                          linewritteln.resize(40);
                            out <<  linewritteln << "\r";
                        } else if ( piecesdir.size() == 1 ) {
                            QString linewritteln = DirIndex + QString(" /") + piecesdir.at(0)  + spaceholder;
                            linewritteln.resize(40);
                            out << linewritteln  << "\r";
                        } else if ( piecesdir.size() == 2 ) {
                            QString linewritteln = DirIndex + QString(" /") + piecesdir.at(1)  + spaceholder;
                            linewritteln.resize(40);
                            out << linewritteln  << "\r";
                        }
                           out.flush();
                      }
                        if (QFileInfo(dirIt.filePath()).isFile()) {
                            if (QFileInfo(dirIt.filePath()).suffix() == EXTENSION ) {
                                 Filefinded++;
                                 const qint64 sizefullfromfile = dirIt.filePath().size();
                                 FoundResults.append(dirIt.filePath());
                                 out << Filefinded << " > " <<  dirIt.filePath()  << ".\n";
                                 out.flush();
                                 /// qDebug()<<dirIt.filePath();
                              }
                        }
            }
            out.flush();
        }
        
        V 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          What do you mean by hang issue ?

          From your description you are at least exploring folders 2000 level deep whith who knows how many files in them so it might indeed take some times before the complete iteration is done.

          V Offline
          V Offline
          Vellayil
          wrote on last edited by
          #4

          Hi,

          Thank you for your replay.

          The hang issue means crash by the stack overflow due to recursion.

          1 Reply Last reply
          0
          • P patrik08

            Hi....
            Here a sample:
            from https://github.com/pehohlva/AnalizeDir/blob/master/robotsystem.cpp

            void Robotsystem::followOneDir( const QString &namepathfull ) {
              QTextStream out(stdout);
              QDirIterator dirIt(namepathfull,QDirIterator::Subdirectories);
              while (dirIt.hasNext()) {
                        dirIt.next();
                        if (!dirIt.filePath().isNull()) {
                            Dirscount++;
                            QString spaceholder;
                            spaceholder.fill(QChar('.'),120);
                            QString fdirx = dirIt.filePath();
                            QStringList piecesdir = fdirx.split( "/" );
                            QString DirIndex= QString("Read %1:").arg(Dirscount);
                            if ( piecesdir.size() > 4 ) {
                              QString linewritteln = DirIndex + QString(" /") + piecesdir.at(3) + spaceholder;
                              linewritteln.resize(40);
                                out <<  linewritteln << "\r";
                            } else if ( piecesdir.size() == 1 ) {
                                QString linewritteln = DirIndex + QString(" /") + piecesdir.at(0)  + spaceholder;
                                linewritteln.resize(40);
                                out << linewritteln  << "\r";
                            } else if ( piecesdir.size() == 2 ) {
                                QString linewritteln = DirIndex + QString(" /") + piecesdir.at(1)  + spaceholder;
                                linewritteln.resize(40);
                                out << linewritteln  << "\r";
                            }
                               out.flush();
                          }
                            if (QFileInfo(dirIt.filePath()).isFile()) {
                                if (QFileInfo(dirIt.filePath()).suffix() == EXTENSION ) {
                                     Filefinded++;
                                     const qint64 sizefullfromfile = dirIt.filePath().size();
                                     FoundResults.append(dirIt.filePath());
                                     out << Filefinded << " > " <<  dirIt.filePath()  << ".\n";
                                     out.flush();
                                     /// qDebug()<<dirIt.filePath();
                                  }
                            }
                }
                out.flush();
            }
            
            V Offline
            V Offline
            Vellayil
            wrote on last edited by
            #5

            @patrik08

            Thank you for your response.

            I think the function "void Robotsystem::followOneDir( const QString &namepathfull )" is give the file list that match.
            The same thing should i get from the following code,

            QDirIterator it(dir, QStringList() << "*.pdf", QDir::Files, QDirIterator::Subdirectories);
            while (it.hasNext())
            {
            qDebug() <<"PDF file : " << it.next();
            }

            My application run on a touch device. The device have 1GB RAM . Application run on LINUX OS.

            Any way QDirIterator have some recursion happens internally due the recursion is there have a possibility to crash the application by stack overflow due to recursion ?.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              patrik08
              wrote on last edited by
              #6

              Yes reursive it self as function or class..

              Its the same as:

              
              /* remove dir recursive */
              void DownDir_RM(const QString d) {
                 QDir dir(d);
                  QDir enddir(d);
                 if (dir.exists())
                 {
                    const QFileInfoList list = dir.entryInfoList();
                    QFileInfo fi;
                    for (int l = 0; l < list.size(); l++)
                    {
                       fi = list.at(l);
                       if (fi.isDir() && fi.fileName() != "." && fi.fileName() != "..")
                          DownDir_RM(fi.absoluteFilePath());
                       else if (fi.isFile())
                       {
                          unlink(fi.absoluteFilePath());
                       }
              
                    }
                       dir.rmdir(d);
                 }
              
                 //// return enddir.exists();
              }
              
              /* remove a file */
              bool unlink(const QString fullFileName)
              {
                  if (fullFileName.size() > 0) {
                  QFile f( fullFileName );
                      if ( f.exists(fullFileName) ) {
                     if (f.remove()) {
                      return true;
                     }
                      }
                  }
              return false;
              }
              
              1 Reply Last reply
              0
              • Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #7

                @Vellayil although from QDirIterator documentation

                It is similar to QDir::entryList() and QDir::entryInfoList(), but because it lists entries one at a time instead of all at once, it scales better and is more suitable for large directories.

                it looks like your walking on an edge case condition. QDirIterator is based on QStack, and as everything in like, it's finite...

                Given your conditions (thousands of files/folders) and restrictions (limited HW) it's advisable you use some defensive programming approach, not relaying entirely in just one QDirIterator for the whole bunch of folders/files you have, but rather having QDirIterator by levels, so if the need arises of going down in the folder hierarchy, you'll be creating QDirIterator for only the levels traversed

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                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