Recursively iterate over all the files in a directory and its subdirectories in Qt(在Qt中递归遍历目录及其子目录中的所有文件)
问题描述
我想递归扫描一个目录及其所有子目录以查找具有给定扩展名的文件 - 例如,所有 *.jpg 文件.你怎么能在 Qt 中做到这一点?
I want to recursively scan a directory and all its sub-directories for files with a given extension - for example, all *.jpg files. How can you do that in Qt?
推荐答案
我建议你看看 QDirIterator.
QDirIterator it(dir, QStringList() << "*.jpg", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
qDebug() << it.next();
您可以简单地递归使用 QDir::entryList(),但 QDirIterator 更简单.此外,如果您碰巧有包含大量文件的目录,您会从 QDir::entryList() 获得非常大的列表,这在小型嵌入式设备上可能不太好.
You could simply use QDir::entryList() recursively, but QDirIterator is simpler. Also, if you happen to have directories with a huge amount of files, you'd get pretty large lists from QDir::entryList(), which may not be good on small embedded devices.
示例(目录为 QDir::currentPath()):
Example (dir is QDir::currentPath()):
luca @ ~/it_test - [] $ tree
.
├── dir1
│ ├── image2.jpg
│ └── image3.jpg
├── dir2
│ └── image4.png
├── dir3
│ └── image5.jpg
└── image1.jpg
3 directories, 5 files
luca @ ~/it_test - [] $ /path/to/app
"/home/luca/it_test/image1.jpg"
"/home/luca/it_test/dir3/image5.jpg"
"/home/luca/it_test/dir1/image2.jpg"
"/home/luca/it_test/dir1/image3.jpg"
这篇关于在Qt中递归遍历目录及其子目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在Qt中递归遍历目录及其子目录中的所有文件
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
