PDA

View Full Version : QDir, list all directories?



hakermania
6th March 2011, 21:20
How can I list/take only directories from a directory?
E.g.


QDir directory = new QDir;
directory.setFolderMode(); //example ! :P
directory.setCurrent("/home/alex/");
for (int i =0; i > directory.count(); i++){
directory.at(i) //mpla mpla...
}

Very sample code, it's just to understand what i want to achieve...
Well, that's what i want to do... Take the subfolder names from a specific dir and perform some actions with each one separately...
Is there any way to do this?

ChrisW67
6th March 2011, 22:24
Look at the filters argument to the QDir constructor or QDir::setFilter()

hakermania
6th March 2011, 22:58
the setfilter takes qstring as argument....Cannot filer the dirs...!

ChrisW67
7th March 2011, 04:12
QDir::setFilter() does not take a QString argument, something you would have noticed if you bothered the read the documentation before declaring that it "Cannot filter the dirs...!". Similarly, the filters argument to the constructor does not take a QString argument.

unit
7th March 2011, 07:22
is setFilter(QDir :: Dirs) (http://doc.trolltech.com/latest/qdir.html#Filter-enum) what you want?

hakermania
11th March 2011, 21:20
For a reason, I get error out of index or something like this with this solution........ :/

wysota
11th March 2011, 21:41
As you said yourself, it's for a reason.

stampede
11th March 2011, 21:44
And the code is ... :p

hakermania
17th May 2012, 08:02
QStringList all_dirs;
all_dirs << parent_folder;
QDirIterator directories(parent_folder, QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);

while(directories.hasNext()){
directories.next();
all_dirs << directories.filePath();
}


Where parent_folder is the folder whose you want all of his subdirectories.

Arshia Aghaei
1st July 2015, 05:36
How can I list/take only directories from a directory?
E.g.


QDir directory = new QDir;
directory.setFolderMode(); //example ! :P
directory.setCurrent("/home/alex/");
for (int i =0; i > directory.count(); i++){
directory.at(i) //mpla mpla...
}

Very sample code, it's just to understand what i want to achieve...
Well, that's what i want to do... Take the subfolder names from a specific dir and perform some actions with each one separately...
Is there any way to do this?

QDir::at() and QDir::setFolderMode() are not real , are they ? because they're not on Qt 5 at least.

jefftee
1st July 2015, 05:41
Look at QDir::entryList() or QDir::entryInfoList() depending on whether you just need the directory names or more info like modification dates, etc.