我想递归扫描一个目录及其所有子目录以查找具有给定扩展名的文件 - 例如,所有 *.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中递归遍历目录及其子目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!