|
|
Getting all files from subdirectories recursively with C++ using Qt Framework Written on January 20, 2009, by Milot Shala. |
Earlier I wrote a media player in C# using .NET framework, and when it got to listing all files in all subdirectories I wrote an recursive algorithm to get all files from the subdirectories and of course it was a bit slow for a big directory of mp3 files because while reading files I extracted the ID3 tags from each file, but then I have used two threads, one for reading filenames, and the second (the base thread) for displaying file names and their extracted ID3 tags to a listbox control. So I came up with the same situation in C++ which I am writing the same player in C++ for cross platform purposes using Qt framework, but right now I am heavily exploring the framework and I love it! Instead of writing a recursive algorithm to list all files, Qt offers a class called QDirIterator which enables you to walk to directories recursively and it is very fast!
Below I am showing you the step by step code how to do it.
Imagine that we have a QDialog and we have a SLOT called selectDirAndGetFiles() (if you are unfamiliar with signals and slots in QT, visit http://doc.trolltech.com/4.4/signalsandslots.html ) and in the constructor of our widget we connect the clicked() SIGNAL to our slot like the code shown below:
MusicWidget::MusicWidget(QWidget *parent = 0) : QDialog( parent ) // call the base class constructor also { connect(ui->buttonGetFiles, SIGNAL(clicked()), this, SLOT(selectDirAndGetFiles())); }
Then we implement our slot:
void MusicWidget::selectDirAndGetFiles() { // Display a dialog to the user to choose his music directory QString directory_path = QFileDialog::getExistingDirectory(this, tr("Select your music directory"), QDir::currentPath()); // Then create an instance of our QDirIterator, which takes as parameters // the directory, a QDir filter and an option flag which the QDirIterator is told // to go on the subdirectories also. // I have combined the QDir filters to list files and not to get the symbolic links (shortcuts in Windows). QDirIterator directory_walker(directory_path, QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories); // QDirIterator object has a boolean method called hasNext() which returns true // if the directory have more files, false otherwise and based on that information, // we can write a while loop like the one below while(directory_walker.hasNext()) { // then we tell our directory_walker object to explicitly take next element until the loop finishes directory_walker.next(); // I want to list just mp3 files! if(directory_walker.fileInfo().completeSuffix() == "mp3") // then we take a filename and display it to a listWidget like the code below: ui->musicListWidget->addItem(directory_walker.fileName()); } }
About Qt Framework
Qt is a great framework which allows you to write cross platform applications. Behind the Qt is NOKIA which also announced that from version 4.5 of Qt framework NOKIA will add LGPL to the Qt’s licensing model, for which I am very happy!
You can read more about this, http://www.qtsoftware.com/about/licensing
If you have comments, requests, complaints, better way to do this, issue discussion on the topic and other related stuff, feel free to comment!
-
Anonymous
4,694 views

