如何使用递归函数打开目录中的子目录?

How to open Subdirectories in a directories by using recursive function?

我的程序已经运行良好,但我想再添加一个条件。该程序打开一个目录并显示 .h 和 .cpp 文件。但是如果有子目录,我看不到子目录中的.cpp 和.h 文件。这是我的代码:

QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
{
    QDir qdir(directory);
    QFileInfoList fileList = qdir.entryInfoList(QStringList() << "*.h"  << "*.hpp" << "*.c" << "*.cpp", QDir::Files | QDir::AllDirs
                                                                                                  | QDir::NoDotAndDotDot);
    QStringList 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);
    }
}

更改 getFileListFromDir() 为子目录递归调用自身。

QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
{
    QDir qdir(directory);
    QFileInfoList fileList = qdir.entryInfoList(QStringList() << "*.h"  << "*.hpp" << "*.c" << "*.cpp", QDir::Files);
    
    for (const QFileInfo &subdir : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
    {  
        fileList << getFileListFromDir(subdir.absoluteFilePath()); // this is the recursion
    }

    return fileList;
}