12

For each OS there is a location for storing temporary data. It could be like: C:/Users/[user name]/AppData/Temp (or so). How can I get this path independently from OS with QT?

3 Answers 3

26

It is not possible to get the temp directory for an arbitrary user, but for the current user you can use QDir::temp() or QDir::tempPath().

2
  • Good point about the arbitrary != current user. The directory for an arbitrary user may in fact be created on demand (common in corporate Win32 environments)
    – MSalters
    Jun 22, 2010 at 10:59
  • @Job The post is a bit old, is there a way to get user's tmp dir? If the system-wide is used, when several users are logged in that causes a collision.
    – quimnuss
    Jul 17, 2013 at 10:02
4

You want to get QDesktopServices::TempLocation. See http://doc.trolltech.com/qtextended4.4/qdesktopservices.html#StandardLocation-enum for details.

2
  • Please note what the current Qt docs say: "The following members of class QDesktopServices are obsolete. They are provided to keep old source code working. We strongly advise against using them in new code." - seems like this member of the class is deprecated...
    – mozzbozz
    Feb 3, 2015 at 16:44
  • The links seems to be down and I can not find this in the official docs (doc.qt.io/qt-5/qdesktopservices.html). Could you please update the link?
    – ph_0
    Jun 15, 2023 at 12:14
2

In Qt 5, you can use QStandardPaths::writableLocation(QStandardPaths::TempLocation) to get the temporary directory path as a QString. You'll need to #include <QStandardPaths> to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.