Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

  • QDir::tempPath()
  • QStandardPaths::writableLocation(QStandardPaths::TempLocation)
like image 762
John Doe Avatar asked Mar 29 '18 19:03

John Doe


1 Answers

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.

like image 117
Kuba hasn't forgotten Monica Avatar answered Oct 19 '22 19:10

Kuba hasn't forgotten Monica