qtqt5qdirqstandardpaths

QDir::tempPath() vs QStandardPaths::writableLocation()


I need to get the path to the temporary directory. Is there any difference between the following methods (except for the first one is available in Qt 4)? Which one is better to use?


Solution

  • TL;DR: Prefer QStandardPaths::writableLocation.

    There is no difference on Unix, OS X and Windows. There, they are guaranteed to always return the same thing. To wit - in qstandardpaths_win.cpp, qstandardpaths_unix.cpp, qstandardpaths_mac.mm, and qstandardpaths_winrt.cpp:

    QString QStandardPaths::writableLocation(StandardLocation type) {
      switch (type) { 
        //[...]
        case TempLocation:
          return QDir::tempPath();
    

    On Android and Haiku, the value returned by QStandardPaths::writableLocation uses a proper system-specific approach, while the value returned by tempPath uses the legacy environment-variable based approach that should be considered deprecated on those systems.