本文介紹了在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中遞歸遍歷目錄及其子目錄中的所有文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!