Qtc++. Improve file search in directory and subdirectories

Question:

There is a code:

 // Получение списка файлов в папке
    QStringList nameFilter;
        QDir dir(MTEPathTMP);
        nameFilter.clear();
        nameFilter << "*.png";

        QFileInfoList list = dir.entryInfoList( nameFilter, QDir::Files );
        QFileInfo fileinfo;

        nameFilter.clear();
        foreach (fileinfo, list) nameFilter << fileinfo.absoluteFilePath();

It searches for files by mask in a directory.

BUT! It does not know how to look in subdirectories. How can I modify it so that it can search?

Answer:

Already discussed here . I would recommend not to reinvent the wheel with a recursive function, but to use the ready-made QDirIterator class with a special flag in the parameters.

QDirIterator it("/sys", QStringList() << "scaling_cur_freq", QDir::NoFilter, QDirIterator::Subdirectories);
while (it.hasNext()) {
    QFile f(it.next());
    f.open(QIODevice::ReadOnly);
    qDebug() << f.fileName() << f.readAll().trimmed().toDouble() / 1000 << "MHz";
}
Scroll to Top