在Qt中,可以使用QDir类来遍历文件夹。以下是使用QDir类遍历文件夹的方法:
创建一个QDir对象,指定要遍历的文件夹路径:QDir dir("/path/to/folder");使用QDir的entryList()方法获取文件夹中的所有文件和子文件夹的列表:QStringList fileList = dir.entryList();遍历文件列表,处理每个文件或文件夹:foreach (QString file, fileList) { // 处理文件或文件夹 qDebug() << file;}完整的示例代码如下:
#include <QCoreApplication>#include <QDir>#include <QDebug>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QDir dir("/path/to/folder"); QStringList fileList = dir.entryList(); foreach (QString file, fileList) { qDebug() << file; } return a.exec();}注意替换/path/to/folder为要遍历的实际文件夹路径。

