我想递归扫描一个目录及其所有子目录以查找具有给定扩展名的文件 - 例如,所有 *.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模板网!
如何在 C++ 中读取和操作 CSV 文件数据?How can I read and manipulate CSV file data in C++?(如何在 C++ 中读取和操作 CSV 文件数据?)
在 C++ 中,为什么我不能像这样编写 for() 循环:In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,为什么我不能像这样编写 for() 循环: for(
OpenMP 如何处理嵌套循环?How does OpenMP handle nested loops?(OpenMP 如何处理嵌套循环?)
在循环 C++ 中重用线程Reusing thread in loop c++(在循环 C++ 中重用线程)
需要精确的线程睡眠.最大 1ms 误差Precise thread sleep needed. Max 1ms error(需要精确的线程睡眠.最大 1ms 误差)
是否需要“do {...} while ()"?环形?Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?环形?)