Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

QDirIterator Filter directory content

From Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s):
Symbian
Article
Created: srikanth_trulyit (19 Jun 2009)
Last edited: hamishwillee (23 Jul 2012)

Contents

Use Case

We may need to filter the directory content. For example we want to list all mp3 files in a path.

QDirIterator is used to iterate through a path and filter all files with a particular suffix.

Code

The following code snippet takes a path and filters as parameters. Returns a list with all files which matched the filter

QStringList MyIterator::getDirContent(const QString& aPath,
QStringList aFilters)
{
// append the filtered files to this list
QStringList list;
 
// set dir iterator
QDirIterator dirIterator(aPath,
aFilters,
QDir::AllDirs|QDir::Files|QDir::NoSymLinks,
QDirIterator::Subdirectories);
while(dirIterator.hasNext())
{
// make sure to call next, failing todo so will result in infinite loop
dirIterator.next();
 
// found required file
if(dirIterator.fileInfo().completeSuffix().contains(KFilterSuffix,Qt::CaseInsensitive))
{
list.append(dirIterator.fileInfo().absoluteFilePath());
}
}
 
return list;
}

Explanation

	QDirIterator dirIterator(aPath,
aFilters,
QDir::AllDirs|QDir::Files|QDir::NoSymLinks,
QDirIterator::Subdirectories);

Setup a directory iterator on the supplied path. In the second argument we set the filters (required file suffixes). Using third argument we set the Directory filters. In this case we check all directories, files and ignore symbolic links. The last argument is iterator flags which is set to iterate sub directories also.


dirIterator.hasNext()

Check if the iterator has any entries

// make sure to call next, failing todo so will result in infinite loop
dirIterator.next();

next() will increment the iterator to the next item. Make sure to call this, else the while loop becomes infinite loop.

Usage

QString musicPath = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
QStringList filters;
filters<<"*.mp3";
QStringList myMp3SongsList = getDirContent(musicPath,filters);
This page was last modified on 23 July 2012, at 12:19.
141 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×