Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
QTemporaryDir Class Reference

\inmodule QtCore \reentrant More...

#include <qtemporarydir.h>

+ Collaboration diagram for QTemporaryDir:

Public Member Functions

 QTemporaryDir ()
 Constructs a QTemporaryDir using as template the application name returned by QCoreApplication::applicationName() (otherwise qt_temp).
 
 QTemporaryDir (const QString &templateName)
 Constructs a QTemporaryDir with a template of templatePath.
 
 QTemporaryDir (QTemporaryDir &&other) noexcept
 Move-constructs a new QTemporaryDir from other.
 
 ~QTemporaryDir ()
 Destroys the temporary directory object.
 
void swap (QTemporaryDir &other) noexcept
 Swaps temporary-dir other with this temporary-dir.
 
bool isValid () const
 Returns true if the QTemporaryDir was created successfully.
 
QString errorString () const
 
bool autoRemove () const
 Returns true if the QTemporaryDir is in auto remove mode.
 
void setAutoRemove (bool b)
 Sets the QTemporaryDir into auto-remove mode if b is true.
 
bool remove ()
 Removes the temporary directory, including all its contents.
 
QString path () const
 Returns the path to the temporary directory.
 
QString filePath (const QString &fileName) const
 

Detailed Description

\inmodule QtCore \reentrant

The QTemporaryDir class creates a unique directory for temporary use.

QTemporaryDir is used to create unique temporary directories safely. The directory itself is created by the constructor. The name of the temporary directory is guaranteed to be unique (i.e., you are guaranteed to not overwrite an existing directory), and the directory will subsequently be removed upon destruction of the QTemporaryDir object. The directory name is either auto-generated, or created based on a template, which is passed to QTemporaryDir's constructor.

Example:

// Within a function/method...
if (dir.isValid()) {
// dir.path() returns the unique directory path
}
// The QTemporaryDir destructor removes the temporary directory
// as it goes out of scope.
\inmodule QtCore \reentrant
QString dir
[11]

It is very important to test that the temporary directory could be created, using isValid(). Do not use \l {QDir::exists()}{exists()}, since a default-constructed QDir represents the current directory, which exists.

The path to the temporary directory can be found by calling path().

A temporary directory will have some static part of the name and some part that is calculated to be unique. The default path will be determined from QCoreApplication::applicationName() (otherwise qt_temp) and will be placed into the temporary path as returned by QDir::tempPath(). If you specify your own path, a relative path will not be placed in the temporary directory by default, but be relative to the current working directory. In all cases, a random string will be appended to the path in order to make it unique.

See also
QDir::tempPath(), QDir, QTemporaryFile

Definition at line 18 of file qtemporarydir.h.

Constructor & Destructor Documentation

◆ QTemporaryDir() [1/3]

QTemporaryDir::QTemporaryDir ( )

Constructs a QTemporaryDir using as template the application name returned by QCoreApplication::applicationName() (otherwise qt_temp).

The directory is stored in the system's temporary directory, QDir::tempPath().

See also
QDir::tempPath()

Definition at line 143 of file qtemporarydir.cpp.

References QTemporaryDirPrivate::create(), and defaultTemplateName().

+ Here is the call graph for this function:

◆ QTemporaryDir() [2/3]

QTemporaryDir::QTemporaryDir ( const QString templatePath)
explicit

Constructs a QTemporaryDir with a template of templatePath.

If templatePath is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath() to construct templatePath if you want use the system's temporary directory.

If the templatePath ends with XXXXXX it will be used as the dynamic portion of the directory name, otherwise it will be appended. Unlike QTemporaryFile, XXXXXX in the middle of the template string is not supported.

See also
QDir::tempPath()

Definition at line 162 of file qtemporarydir.cpp.

References QTemporaryDirPrivate::create(), defaultTemplateName(), and QString::isEmpty().

+ Here is the call graph for this function:

◆ QTemporaryDir() [3/3]

QTemporaryDir::QTemporaryDir ( QTemporaryDir &&  other)
inlinenoexcept

Move-constructs a new QTemporaryDir from other.

Note
The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.
Since
6.4

Definition at line 23 of file qtemporarydir.h.

References other().

+ Here is the call graph for this function:

◆ ~QTemporaryDir()

QTemporaryDir::~QTemporaryDir ( )

Destroys the temporary directory object.

If auto remove mode was set, it will automatically delete the directory including all its contents.

See also
autoRemove()

Definition at line 211 of file qtemporarydir.cpp.

References QTemporaryDirPrivate::autoRemove, and remove().

+ Here is the call graph for this function:

Member Function Documentation

◆ autoRemove()

bool QTemporaryDir::autoRemove ( ) const

Returns true if the QTemporaryDir is in auto remove mode.

Auto-remove mode will automatically delete the directory from disk upon destruction. This makes it very easy to create your QTemporaryDir object on the stack, fill it with files, do something with the files, and finally on function return it will automatically clean up after itself.

Auto-remove is on by default.

See also
setAutoRemove(), remove()

Definition at line 289 of file qtemporarydir.cpp.

References QTemporaryDirPrivate::autoRemove.

◆ errorString()

QString QTemporaryDir::errorString ( ) const
Since
5.6

If isValid() returns false, this function returns the error string that explains why the creation of the temporary directory failed. Otherwise, this function return an empty string.

Definition at line 236 of file qtemporarydir.cpp.

References QTemporaryDirPrivate::pathOrError, and QTemporaryDirPrivate::success.

◆ filePath()

QString QTemporaryDir::filePath ( const QString fileName) const
Since
5.9

Returns the path name of a file in the temporary directory. Does not check if the file actually exists in the directory. Redundant multiple separators or "." and ".." directories in fileName are not removed (see QDir::cleanPath()). Absolute paths are not allowed.

Definition at line 259 of file qtemporarydir.cpp.

References fileName, QDir::isAbsolutePath(), QTemporaryDirPrivate::pathOrError, qUtf8Printable, qWarning, ret, and QTemporaryDirPrivate::success.

+ Here is the call graph for this function:

◆ isValid()

bool QTemporaryDir::isValid ( ) const

Returns true if the QTemporaryDir was created successfully.

Definition at line 224 of file qtemporarydir.cpp.

References QTemporaryDirPrivate::success.

Referenced by QQmlDataTest::QQmlDataTest().

+ Here is the caller graph for this function:

◆ path()

QString QTemporaryDir::path ( ) const

Returns the path to the temporary directory.

Empty if the QTemporaryDir could not be created.

Definition at line 245 of file qtemporarydir.cpp.

References QTemporaryDirPrivate::pathOrError, and QTemporaryDirPrivate::success.

Referenced by QQmlDataTest::QQmlDataTest(), and remove().

+ Here is the caller graph for this function:

◆ remove()

bool QTemporaryDir::remove ( )

Removes the temporary directory, including all its contents.

Returns true if removing was successful.

Definition at line 311 of file qtemporarydir.cpp.

References path(), Q_ASSERT, qWarning, QDir::removeRecursively(), QTemporaryDirPrivate::success, and QDir::toNativeSeparators().

Referenced by ~QTemporaryDir().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAutoRemove()

void QTemporaryDir::setAutoRemove ( bool  b)

Sets the QTemporaryDir into auto-remove mode if b is true.

Auto-remove is on by default.

See also
autoRemove(), remove()

Definition at line 301 of file qtemporarydir.cpp.

References QTemporaryDirPrivate::autoRemove.

◆ swap()

void QTemporaryDir::swap ( QTemporaryDir other)
inlinenoexcept

Swaps temporary-dir other with this temporary-dir.

This operation is very fast and never fails.

Since
6.4

Definition at line 31 of file qtemporarydir.h.

References other(), and qt_ptr_swap().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: