Recursively iterate over all the files in a directory and its subdirectories in Qt

前端 未结 4 1363
醉梦人生
醉梦人生 2020-12-07 15:47

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?

4条回答
  •  一整个雨季
    2020-12-07 16:28

    I suggest you have a look at QDirIterator.

    QDirIterator it(dir, QStringList() << "*.jpg", QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext())
        qDebug() << it.next();
    

    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.

提交回复
热议问题