6.0.0-rc4Source

QTemporaryDir Class

The QTemporaryDir class creates a unique directory for temporary use. More…

Header#include <QTemporaryDir>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QTemporaryDir()
QTemporaryDir(const QString &templatePath)
QTemporaryDir(QTemporaryDir &&other)
QTemporaryDir &operator=(QTemporaryDir &&other)
~QTemporaryDir()
boolisValid() const
QStringerrorString() const
boolautoRemove() const
voidsetAutoRemove(bool b)
boolremove()
QStringpath() const
QStringfilePath(const QString &fileName) const

Detailed Description

QTemporaryDir creates a uniquely named directory in its constructor and, by default, removes it with all of its contents when the object is destroyed. Check isValid() before using path().

The directory name is built from a template. The six characters XXXXXX in the last path component are replaced by random characters. A template without XXXXXX gets -XXXXXX appended. The default template is qt_temp-XXXXXX in QDir::tempPath(). The directory is created with mode 0700.

QTemporaryDir dir;
if (dir.isValid()) {
    QFile file(dir.filePath("output.txt"));
    file.open(QIODevice::WriteOnly);
}

A QTemporaryDir can be moved but not copied.

See also QTemporaryFile and QDir.

Differences from Qt

path() is always an absolute path, even for a relative template.

Member Function Documentation

QTemporaryDir::QTemporaryDir()

Constructs a QTemporaryDir that creates a directory using the default template, qt_temp-XXXXXX in QDir::tempPath(). Use isValid() to check whether the directory was created.

[explicit] QTemporaryDir::QTemporaryDir(const QString &templatePath)

Constructs a QTemporaryDir that creates a directory from templatePath. If templatePath is empty, the default template is used. Use isValid() to check whether the directory was created.

QTemporaryDir::QTemporaryDir(QTemporaryDir &&other)

Move-constructs a QTemporaryDir from other. The directory and its auto-remove setting are transferred; other becomes invalid and no longer removes anything.

QTemporaryDir::~QTemporaryDir()

Destroys the object. If autoRemove() is true and the directory is valid, it is removed with all its contents.

See also remove().

bool QTemporaryDir::autoRemove() const

Returns true if the directory is removed when the object is destroyed; otherwise returns false. The default is true.

See also setAutoRemove() and remove().

QString QTemporaryDir::errorString() const

Returns a description of the error that prevented the directory from being created, or an empty string if there was none.

See also isValid().

QString QTemporaryDir::filePath(const QString &fileName) const

Returns the path to fileName inside the temporary directory. The file is not created. Returns an empty string if the directory is not valid or fileName is an absolute path.

See also path().

bool QTemporaryDir::isValid() const

Returns true if the temporary directory was created successfully; otherwise returns false. The directory is also invalid after being moved from.

See also errorString() and path().

QTemporaryDir & QTemporaryDir::operator=(QTemporaryDir &&other)

Move-assigns other to this object and returns a reference to it. The directory currently held, if any and if auto-remove is enabled, is removed first.

QString QTemporaryDir::path() const

Returns the absolute path of the temporary directory, or an empty string if the directory is not valid.

See also filePath() and isValid().

bool QTemporaryDir::remove()

Removes the directory and all its contents recursively. Returns true if successful; returns false if the directory is not valid or the removal fails.

See also isValid() and autoRemove().

void QTemporaryDir::setAutoRemove(bool b)

Sets whether the directory is removed when the object is destroyed to b.

See also autoRemove().