Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reading subfolders
Qt 6.12 LTS is out! See what's new in the release blog

Reading subfolders

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.5k 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.
  • eefesafakE Offline
    eefesafakE Offline
    eefesafak
    wrote on last edited by
    #1

    My program is created for reading all .cpp and .h files then count line numbers in a folder. But I want to add one more condition which is check that if there is more subfolders. For example, If I want to read /D folder, the program should read all of the /D folder's .cpp and .h files and subfolders which contains .cpp and .h files. Afterward, all of the files must be seen in the program. Here is my program and codes:

    Screenshot from 2022-05-06 11-32-53.png
    Screenshot from 2022-05-06 11-33-11.png

    void MainWindow::addItemToList(const QString &file, const int &fileSize, const int &count)
    {
        QTableWidgetItem *iFileName = new QTableWidgetItem(file);
        QTableWidgetItem *iFileSize = new QTableWidgetItem(QString::number(fileSize));
        QTableWidgetItem *iFileCount = new QTableWidgetItem(QString::number(count));
    
        int rowCount = ui->tableWidget->rowCount();
    
        ui->tableWidget->insertRow(rowCount);
    
        rowCount = ui->tableWidget->rowCount();
    
        ui->tableWidget->setItem(rowCount-1, 0, iFileName);
        ui->tableWidget->setItem(rowCount-1, 1, iFileSize);
        ui->tableWidget->setItem(rowCount-1, 2, iFileCount);
    }
    
    QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
    {
        QDir qdir(directory);
        QFileInfoList fileList = qdir.entryInfoList(QStringList() << "*.h"  << "*.hpp" << "*.c" << "*.cpp", QDir::Files);
        return fileList;
    }
    
    void MainWindow::on_Browse_clicked()
    {
        QString path = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly |
                                                                                              QFileDialog::DontResolveSymlinks);
    
        if(path.isEmpty())
            return;
    
        ui->FullPath->setText(path);
    }
    
    void MainWindow::on_Ok_clicked()
    {
        QString path = ui->FullPath->text();
        if(path.isEmpty())
            return;
    
        ui->tableWidget->setRowCount(0);
    
        QFileInfoList fileList = getFileListFromDir(path);
    
        int count = 0;
        foreach(const QFileInfo& file, fileList)
        {
            count = m_ig->funcCountLines(file.filePath());
            addItemToList(file.filePath(), file.size(), count);
        }
    }
    
    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QDir::entryInfoList() can also return the subdirectories.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      eefesafakE 1 Reply Last reply
      1
      • eefesafakE eefesafak

        My program is created for reading all .cpp and .h files then count line numbers in a folder. But I want to add one more condition which is check that if there is more subfolders. For example, If I want to read /D folder, the program should read all of the /D folder's .cpp and .h files and subfolders which contains .cpp and .h files. Afterward, all of the files must be seen in the program. Here is my program and codes:

        Screenshot from 2022-05-06 11-32-53.png
        Screenshot from 2022-05-06 11-33-11.png

        void MainWindow::addItemToList(const QString &file, const int &fileSize, const int &count)
        {
            QTableWidgetItem *iFileName = new QTableWidgetItem(file);
            QTableWidgetItem *iFileSize = new QTableWidgetItem(QString::number(fileSize));
            QTableWidgetItem *iFileCount = new QTableWidgetItem(QString::number(count));
        
            int rowCount = ui->tableWidget->rowCount();
        
            ui->tableWidget->insertRow(rowCount);
        
            rowCount = ui->tableWidget->rowCount();
        
            ui->tableWidget->setItem(rowCount-1, 0, iFileName);
            ui->tableWidget->setItem(rowCount-1, 1, iFileSize);
            ui->tableWidget->setItem(rowCount-1, 2, iFileCount);
        }
        
        QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
        {
            QDir qdir(directory);
            QFileInfoList fileList = qdir.entryInfoList(QStringList() << "*.h"  << "*.hpp" << "*.c" << "*.cpp", QDir::Files);
            return fileList;
        }
        
        void MainWindow::on_Browse_clicked()
        {
            QString path = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly |
                                                                                                  QFileDialog::DontResolveSymlinks);
        
            if(path.isEmpty())
                return;
        
            ui->FullPath->setText(path);
        }
        
        void MainWindow::on_Ok_clicked()
        {
            QString path = ui->FullPath->text();
            if(path.isEmpty())
                return;
        
            ui->tableWidget->setRowCount(0);
        
            QFileInfoList fileList = getFileListFromDir(path);
        
            int count = 0;
            foreach(const QFileInfo& file, fileList)
            {
                count = m_ig->funcCountLines(file.filePath());
                addItemToList(file.filePath(), file.size(), count);
            }
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @eefesafak said in Reading subfolders:

        QFileInfoList fileList = qdir.entryInfoList(QStringList() << "*.h" << "*.hpp" << "*.c" << "*.cpp", QDir::Files);

        This only fetches files. Somewhere you need to use similar to fetch directories/folders, and recurse on them finding sub-folders/files.

        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          QDir::entryInfoList() can also return the subdirectories.

          eefesafakE Offline
          eefesafakE Offline
          eefesafak
          wrote on last edited by
          #4

          @Christian-Ehrlicher How can i do that by using entryInfoList()? Do you have any suggestions?

          Christian EhrlicherC jsulmJ 2 Replies Last reply
          0
          • eefesafakE eefesafak

            @Christian-Ehrlicher How can i do that by using entryInfoList()? Do you have any suggestions?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @eefesafak said in Reading subfolders:

            How can i do that by using entryInfoList()?

            I don't understand this question. When you find a subdirectory then call entryInfoList on this directory.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • eefesafakE eefesafak

              @Christian-Ehrlicher How can i do that by using entryInfoList()? Do you have any suggestions?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @eefesafak said in Reading subfolders:

              How can i do that by using entryInfoList()

              Please read documentation!
              Currently you only read files by passing QDir::Files.
              You can also read directories: QDir::Files | QDir::AllDirs
              QDir::AllDirs ignores the name filter (as mentioned in the documentation), so you will also get all directories.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              1
              • jsulmJ jsulm

                @eefesafak said in Reading subfolders:

                How can i do that by using entryInfoList()

                Please read documentation!
                Currently you only read files by passing QDir::Files.
                You can also read directories: QDir::Files | QDir::AllDirs
                QDir::AllDirs ignores the name filter (as mentioned in the documentation), so you will also get all directories.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @jsulm said in Reading subfolders:

                You can also read directories: QDir::Files | QDir::AllDirs

                @eefesafak
                Probably easiest for you if you use QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot.

                You either pass this on your existing call to get directories, as well as files matching the pattern, and then use QFileInfo::isDir() to recognise which returned entries are directories so you can recurse on them, or issue a separate call with QDir::AllDirs | QDir::NoDotAndDotDot to deal with the sub-directories only.

                eefesafakE 1 Reply Last reply
                2
                • JonBJ JonB

                  @jsulm said in Reading subfolders:

                  You can also read directories: QDir::Files | QDir::AllDirs

                  @eefesafak
                  Probably easiest for you if you use QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot.

                  You either pass this on your existing call to get directories, as well as files matching the pattern, and then use QFileInfo::isDir() to recognise which returned entries are directories so you can recurse on them, or issue a separate call with QDir::AllDirs | QDir::NoDotAndDotDot to deal with the sub-directories only.

                  eefesafakE Offline
                  eefesafakE Offline
                  eefesafak
                  wrote on last edited by
                  #8

                  @JonB I wrote;

                  QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot
                  

                  But the program shows only the folder(/home/efe/ImageInverter/efe):
                  Screenshot from 2022-05-06 12-54-13.png

                  For example, I want to see "/home/efe/ImageInverter/efe/main.cpp".

                  jsulmJ 1 Reply Last reply
                  0
                  • eefesafakE eefesafak

                    @JonB I wrote;

                    QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot
                    

                    But the program shows only the folder(/home/efe/ImageInverter/efe):
                    Screenshot from 2022-05-06 12-54-13.png

                    For example, I want to see "/home/efe/ImageInverter/efe/main.cpp".

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @eefesafak said in Reading subfolders:

                    For example, I want to see "/home/efe/ImageInverter/efe/main.cpp".

                    That's why you need to reqursevly go into subfolders as @JonB already suggested.
                    This is a popular programming exercise by the way.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    3

                    • Login

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