Has anyone seen QDirIterator?

Published Thursday December 14th, 2006
2 Comments on Has anyone seen QDirIterator?
Posted in KDE, Qt, Qtopia

Recently QDirIterator showed up in the Qt 4.3 snapshots. Doesn’t have that many bells and whistles, but it’s a pretty useful tool. Here’s how to use it:

QDirIterator it("testDir/");
while (it.hasNext()) {
    qDebug() < < it.next();

    // "testDir/."
    // "testDir/.."
    // "testDir/directory"
    // "testDir/file.txt"
    // "testDir/otherFile.txt"
    // "testDir/otherDirectory"
    // "testDir/symlink"
    // ...
}

It provides a convenient way of stepping through all entries in a directory. Much like QDir::entryList(), but it's different in two areas:

1) It's an iterator, giving you one entry at a time, and the first entry is available immediately. That means it scales much better than QDir::entryList(), which loads all entries in a directory into memory at once, before the results are available. (For very large directories, that can take some time, and it allocates memory.)

2) It supports subdirectory recursion, and following symlinks. Take the above example, and add a flag:

QDirIterator it("/home/ahanssen", QDirIterator::Subdirectories);
while (it.hasNext()) {
    qDebug() < < it.next();

    // "testDir/."
    // "testDir/.."
    // "testDir/directory"
    // "testDir/directory/."
    // "testDir/directory/.."
    // "testDir/directory/fileInDirectory.txt"
    // "testDir/directory/dirInDirectory/otherfile.txt"
    // "testDir/file.txt"
}

Now you don't need to write recursive functions to do operations on all entries in a directory. If you add QDirIterator::FollowSymlinks, it will recurse into symlinked directories too (it even resolves shortcuts on Windows). And it skips symlink loops (link -> "." or link -> ".." and so on).

I don't know about you but... I think it's a neat class :-).

The fun part is when you run this thing on UNC shares on Windows, or against local Qt resources. Iterate over ":/", for example, adding the subdirectory flag, and see for yourself. ๐Ÿ™‚

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in KDE, Qt, Qtopia

2 comments

Leo S says:

This type of class is why Qt makes me so productive. It’s not rocket surgery, but it just makes sense. How many times have I written a recursive directory iterator and then wondered why this common operation isn’t done for me. Qt makes common things like this effortless. It Just Makes Sense.

Will Stokes says:

I couldn’t agree with you more. I can’t wait to take advantage of this in my slide show app. I currently do have a recursive directory traverser and I have seen how dang expensive it can be to get the contents for a BIG directory, and I’m not taking recursively, just a directory a lot of files and folders in it. Maybe it’s just me, but I often feel like really useful little things like this are more of a Qt4 thing. Qt3 programming was so much harder than Qt4 coding has become it’s almost scary.

Commenting closed.

Get started today with Qt Download now