Sunday, January 9, 2011

List directories in Qt

This code snippet allows programmer to list directories in a parent directory "C:\temp" by using Qt API

#include <QDir>

void list(QStringList& dirnames)
{

         QDir currentDir("C:\\temp");

currentDir.setFilter(QDir::Dirs);
QStringList entries = currentDir.entryList();
for( QStringList::ConstIterator entry=entries.begin(); entry!=entries.end(); ++entry )
{
//std::cout << *entry << std::endl;
QString dirname=*entry;
if(dirname != tr(".") && dirname != tr(".."))
{
dirnames.add(dirname);
}
}
}

1 comment: