Qt program is a method to obtain the path where the program is located, user directory path, temporary folder and other special paths

We often need to access some special paths in our program, such as the path where the program is located, user directory path, temporary folder, etc. Although the methods used to realize these functions in Qt are not difficult, they are different. It is inconvenient to check them every time. So I wrote this blog to summarize the implementation methods of these requirements. It's a memo.

Program path
Get the path where the program is located, and the related functions are implemented in QCoreApplication class:

QString QCoreApplication::applicationDirPath()

For example, we have a program:

C:/Qt/examples/tools/regexp/regexp.exe

Then the result of QAPP - > applicationdirpath() is:

C:/Qt/examples/tools/regexp

If we want the full name of the program in addition to the path where the program is located. Then it can be written as follows:

qApp->applicationFilePath()

Or the above example, the result is:

C:/Qt/examples/tools/regexp/regexp.exe

Current working directory
QDir provides a static function currentPath() to obtain the current working directory. The function prototype is as follows:

QString QDir::currentPath()

If we double-click a program to run, the working directory of the program is the directory where the program is located.

If you run a program on the command line, the directory on the command line when running the program is the current directory.

User directory path
Method in Qt 4. The following method is only valid for Qt 4. Qt 5 has deleted the storageLocation() method.

QDesktopServices::storageLocation(QDesktopServices::HomeLocation);

The method introduced in Qt 5.

QStandardPaths::writableLocation(QStandardPaths::HomeLocation);

perhaps

QStandardPaths::standardLocations(QStandardPaths::HomeLocation);

The difference between the two methods is that the return value of standardLocations() is QStringList. Of course, for HomeLocation, there is only one QString in the QStringList.

There is another way to use a static function of QDir class:

QDir::homePath();

My document path
Method in Qt 4. The following method is only valid for Qt 4. Qt 5 has deleted the storageLocation() method.

QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);

The method introduced in Qt 5.

QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);

Desktop path
Method in Qt 4. The following method is only valid for Qt 4. Qt 5 has deleted the storageLocation() method.

QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);

The method introduced in Qt 5.

QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);

Program data storage path
Usually we will store some data required by the program into the registry. But sometimes too much data needs to be stored, so it is not suitable to put it in the registry. At this time, we need to find a special place to put the data. In the past, I liked to put the data directly into the directory where the program is located, but later I found that when my program runs, I often didn't have permission to write to the files in this directory. Later, it was found that Qt had already considered these problems for us.

Method in Qt 4. The following method is only valid for Qt 4. Qt 5 has deleted the storageLocation() method.

QDesktopServices::storageLocation(QDesktopServices::DataLocation);
The method introduced in Qt 5.

QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);

Another method is introduced in Qt 5.5:

QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation);

Generally speaking, the result of this method is the same as that of the above method. As explained in the Qt help document, this method can ensure that the returned path is not empty. So I think this method should be preferred.

Temporary file path
Method in Qt 4. The following method is only valid for Qt 4. Qt 5 has deleted the storageLocation() method.

QDesktopServices::storageLocation(QDesktopServices::TempLocation);
The method introduced in Qt 5.

QStandardPaths::writableLocation(QStandardPaths::TempLocation);
QStandardPaths::standardLocations(QStandardPaths::TempLocation);

A more traditional approach is to use a static function tempPath() of QDir.

QDir::tempPath();

Two other classes are required to generate temporary files and temporary directories in this directory: QTemporaryFile and QTemporaryDir. I won't introduce it. You can refer to qt's help document.

So far, the commonly used special paths are almost introduced. There are still some that are not commonly used. Please refer to the introduction of QStandardPaths class.

Added by hailam on Sat, 01 Jan 2022 06:32:02 +0200