5

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?

John Doe
  • 555
  • 6
  • 17
  • I see that both point to the same path, so use either. – eyllanesc Mar 29 '18 at 19:13
  • 1
    @eyllanesc The path is also the same for me, however probably there are some OS-specific differences under the hood, that's why I'm asking. – John Doe Mar 29 '18 at 19:17
  • 1
    I would be grateful if someone exaplain me the reason of -1: I'm new here and I'm ready for a critisim, however the silent -1 seems unconstructive to me. – John Doe Mar 29 '18 at 19:18
  • 1
    The documentation in both are very clear, indicate the values ​​they take. I do not know what solution or magic answer you expect. What kind of answers are you waiting for ?, You could check the source code and probably find `QDir::tempPath()` use `QStandardPaths::writableLocation(QStandardPaths::TempLocation)` in Qt5 or its similar if it is in Qt4 – eyllanesc Mar 29 '18 at 19:20
  • 2
    Yes it is OS specific and many of the Qt standard paths are kinda iffy too. But it gets even worse if you try to get volumes info on say... android... – dtech Mar 29 '18 at 21:03

1 Answers1

6

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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Is it stated somewhere in the Android docs that using TMPDIR (used by `QDir::tempPath`) is deprecated? Note that I filed a bug report (https://bugreports.qt.io/browse/QTBUG-98502) to implement both functions identical on Android. – m7913d Nov 22 '21 at 22:32