Recorre un directorio de forma recursiva en Qt, omite las carpetas "." y ".."

I have a little trouble using the Qt functions to walk through a directory recursively. What I'm trying to do:

Open a specified directory. Walk through the directory, and each time it encounters another directory, open that directory, walk through the files, etc.

Now, how I am going about this:

QString dir = QFileDialog::getExistingDirectory(this, "Select directory");
if(!dir.isNull()) {

void Mainwindow::ReadDir(QString path) {
    QDir dir(path);                            //Opens the path
    QFileInfoList files = dir.entryInfoList(); //Gets the file information
    foreach(const QFileInfo &fi, files) {      //Loops through the found files.
        QString Path = fi.absoluteFilePath();  //Gets the absolute file path
        if(fi.isDir()) ReadDir(Path);          //Recursively goes through all the directories.
        else {
            //Do stuff with the found file.

Now, the actual problem I'm facing: Naturally, entryInfoList would also return the '.' and '..' directories. With this setup, this proves a major problem.

By going into '.', it would go through the entire directory twice, or even infinitely (because '.' is always the first element), with '..' it would redo the process for all folders beneath the parent directory.

I would like to do this nice and sleek, is there any way to go about this, I am not aware of? Or is the only way, that I get the plain filename (without the path) and check that against '.' and '..'?

preguntado el 28 de agosto de 12 a las 12:08

1 Respuestas

Deberías intentar usar el QDir::NoDotAndDotDot filtrar en tu entryInfoList, como se describe en el documentación.


  • No olvides añadir un QDir::Fileso QDir::Dirs or QDir::AllFiles to pick up the files and/or directories, as described en este post.

  • Es posible que también desee verificar esta pregunta anterior.

contestado el 23 de mayo de 17 a las 13:05

Yes, and as described aquí, the QT needs to be QDir, and the Filter needs to be expanded with QDir::AllEntries. You're still getting the 'correct', because you pointed me in the right direction. Thank you :) - Aaylor

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.