Qt program about path, user directory path, temporary folder location acquisition method

 For example, we have a program in:

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

1. The directory where the program is located

  QString QCoreApplication::applicationDirPath()

  Then the result of qApp->applicationDirPath() is:

  输出:C:/Qt/examples/tools/regexp  

2.  The full name of the program. Then you can write:

  qApp->applicationFilePath()

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

3. Current working directory

    QDir provides a static function currentPath() to get the current working directory

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

  If you run a program under the command line, then which directory on the command line is when the program is run, that directory is the current directory.

4. User directory path

  Methods introduced in Qt 5

   QStandardPaths::writableLocation(QStandardPaths::HomeLocation);

  QStandardPaths::standardLocations(QStandardPaths::HomeLocation);

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

  There is another way, using a static function of the QDir class:

  QDir::homePath();

 

5. My document path

   Method introduced in Qt 5.

  QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);

  QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);

6. Desktop path

  QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);

  QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);

7. Program data storage path

  Usually we will store some data required by the program in the registry. But sometimes the data that needs to be stored is too much to be placed 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 my program often did not have permission to write the files in this directory when it was running. Later, I found out that Qt has already considered these problems for us.

  Method introduced in Qt 5.

  QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);

  QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);

  Another method was introduced in Qt 5.5:  

  QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);

  QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation);

  This method generally yields the same results as the methods above. As explained in the Qt help documentation, this method ensures that the returned path is not empty. So I think this method should be preferred.

8.  Temporary file path

  Method introduced in Qt 5.

  QStandardPaths::writableLocation(QStandardPaths::TempLocation);

  QStandardPaths::standardLocations(QStandardPaths::TempLocation);

  A more traditional approach is to use one of QDir's static functions, tempPath().

  QDir::tempPath();

  在这个目录下生成临时文件和临时目录需要用到另外两个类: QTemporaryFile 和 QTemporaryDir。就不展开介绍了,大家可以参考 qt 的帮助文档。

至此,常用的各种特殊路径就介绍的差不多了。剩下还有些不常用的,可以参考 QStandardPaths 类的介绍。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518209&siteId=291194637