What is new in Qt 5: QStandardPaths

What's new in Qt 5: QStandardPaths

By ICS Development Team

Qt 5.0.0 introduced the QStandardPaths class. This expands on functionality that was in Qt 4 in the QDesktopServices class.

The QStandardPaths class provides methods for accessing standard paths on a system such as the location of the user's desktop directory or location of a program executable. The class provides only static methods, so it never needs to be instantiated as an object.

I wrote a small Qt-based graphical application that illustrates many of the features of the class. A screen shot is shown below.

Snapshot0

 

Given one of about fifteen location types, it can display the standard locations for that type. For example, on my Linux desktop system for "Application Data" it returns these directories:

 

Snapshot1
 

This calls QStandardPaths::standardLocations(). Note that it can return multiple directories. There is also a method QStandardPaths::writableLocation() which returns a user-writable location for a location type. For the example above, my system returns/home/tranter/.local/share/qstandardpaths.

Given the name of an executable, like "qmake", QStandardPaths::findExecutable()tries to find the path to the program. In my case, calling it for "qmake" returns:

 

Snapshot2

 

The method locate() will attempt to find a file in a standard location. For example, calling it with the filename ".profile" and a location type of Home, it returns the full path to the login profile in my home directory:

 

Snapshot3
 

You can specify whether to look for a file or directory.

The similar method locateAll() will return all files with a given name in a specified standard location.

The class supports a test mode which can be enabled or disabled. This is used by the auto tests for the class to use test directories and to avoid changing the current user's configuration files.

In summary, the new QStandardPaths class is a useful addition in Qt 5 and makes it possible to integrate with desktop systems in a portable way.

You can download a zip archive of the source files for the application from here.