Handling Files and Directories

Qt's QDir class provides a platform-independent means of traversing directories and retrieving information about files. To see how QDir is used, we will write a small console application that calculates the space consumed by all the images in a particular directory and all its subdirectories to any depth.

The heart of the application is the imageSpace() function, which computes the size of a given directory:

int imageSpace(const QString &path) { QDir dir(path); QStringList::Iterator it; int size = 0; QStringList files = dir.entryList("*.png *.jpg *.jpeg", QDir::Files); it = files.begin(); while (it != files.end()) { size += QFileInfo(path, *it).size(); ++it; } QStringList dirs = dir.entryList(QDir::Dirs); it = dirs.begin(); while (it != dirs.end()) { if (*it != "." && *it != "..") size += imageSpace(path + "/" + *it); ++it; } return size; }

We begin by creating a QDir object using the given path. We pass the entryList() function two arguments. The first is a space-separated list of file name filters. These can contain '*' and '?' wildcard characters. In this example, we are filtering to include only PNG and JPEG files. The second argument specifies what kind of entries we want (normal files, directories, drives, etc.).

We iterate over the list of files, accumulating their sizes. The QFileInfo class allows us to access a file's attributes, such as its size, permissions, owner, and timestamps.

The second entryList() call retrieves all the subdirectories in this directory. We iterate over them and recursively call imageSpace() to ascertain their accumulated image sizes.

To create each subdirectory's path, we combine the current directory's path with the subdirectory name (*it), separating them with a slash. QDir treats'/' as a directory separator on all platforms, in addition to '' on Windows. When presenting paths to the user, we can call the static function QDir::convertSeparators() to convert slashes to the correct platform-specific separator.

Let's add a main() function to our small program:

int main(int argc, char *argv[]) { QString path = QDir::currentDirPath(); if (argc > 1) path = argv[1]; cerr << "Space used by images in " << endl << path.ascii() << endl << "and its subdirectories is" << (imageSpace(path) / 1024) << " KB" << endl; return 0; }

For this example, we don't need a QApplication object, because we are only using Qt's tool classes. See http://doc.trolltech.com/3.2/tools.html for the list of these classes.

We use QDir::currentDirPath() to initialize the path to the current directory. Alternatively, we could have used QDir::homeDirPath() to initialize it to the user's home directory. If the user has specified a path on the command line, we use that instead. Finally, we call our imageSpace() function to calculate how much space is consumed by images.

The QDir class provides other file and directory-related functions, including rename(), exists(), mkdir(), and rmdir(). The QFile class provides some static convenience functions, including remove() and exists().

Категории