How to read all file names in a folder using Qt?

In Qt, you can utilize the QDir class to retrieve all file names within a folder. Below is an example code:

#include <QCoreApplication>
#include <QDir>
#include <QDebug>

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

    QString folderPath = "path/to/folder"; // 替换为实际文件夹路径

    QDir folder(folderPath);
    QStringList fileNames = folder.entryList(QDir::Files);

    foreach (QString fileName, fileNames) {
        qDebug() << fileName;
    }

    return a.exec();
}

In the example code above, a QDir object is first created to represent the folder that needs to be read. Then, the entryList() function is used to retrieve all the file names in the folder and store them in a QStringList. Finally, a foreach loop is used to iterate through the list of file names and print out each file name.

It is important to replace “path/to/folder” with the actual folder path.

bannerAds