List folders and files in Qt Creator

3

I want to make a program that tells me what folders and files I have within a given route. The code that I used is the following:

#include <QDir>
#include <QDebug>

int main(){
     QStringList lista = QDir("C:/Imagenes").entryList();
     qDebug() << lista;
     return 0;
}

The "Images" folder contains two images and a folder as you can see in the following image:

Folder "Images"

When executing the program, I get the following output:

My question is why you get it as an exit "." and ".." in addition to the files and folders that are in the directory and how you could get only to list the two images and the "New" folder.

Thank you!

    
asked by Adrian 03.10.2017 в 16:25
source

1 answer

3

One possible solution: using another of the constructors provided by QDir :

#include <QDir>
#include <QDebug>

int main(){
  QStringList lista = QDir("C:/Imagenes", QString( ), SortFlags( Name | IgnoreCase ), Filters( NoDotAndDotDot ) ).entryList();
  qDebug() << lista;
  return 0;
}

As you can see, the only changes are the extra arguments passed to the constructor. QDir::NoDotAndDotDot serves to do precisely what you ask: exclude special entries . and .. .

More information about the constructor here

    
answered by 03.10.2017 / 16:33
source