Quick Tip: Qt 5 Standard Paths

In Qt 5, the way you retrieve standard paths for a system has changed. Now, you use a special class named QStandardPaths instead of the desktop services class. By far my favorite use for this is locating directories without user interaction. In this quick example, we’ll locate the user’s home directory. Note that you can use any of the provided enums in replace of QStandardPaths::HomeLocation.

QString homeLocation = QStandardPaths::locate(QStandardPaths::HomeLocation, QString(), QStandardPaths::LocateDirectory);

This will return C:/Users/Admin on my development PC. This should work cross platform as well, so no more fiddling with the preprocessor to decide which default location to use. 🙂 You can replace the QString() with additional details for location.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s