6.0.0-rc4Source

QTemporaryFile Class

The QTemporaryFile class is an I/O device that operates on temporary files. More…

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

List of all members, including inherited members

Public Functions

QTemporaryFile()
QTemporaryFile(const QString &templateName)
QTemporaryFile(QObject *parent)
QTemporaryFile(const QString &templateName, QObject *parent)
~QTemporaryFile()
boolautoRemove() const
voidsetAutoRemove(bool b)
boolopen()
QStringfileTemplate() const
voidsetFileTemplate(const QString &name)
QStringfileName() const
boolrename(const QString &newName)

Static Public Members

QTemporaryFile *createNativeFile(QFile &file)
QTemporaryFile *createNativeFile(const QString &fileName)

Reimplemented Protected Functions

boolopen(OpenMode mode)

Detailed Description

QTemporaryFile creates a uniquely named file that is safe to use from several processes. The file is created by open() and, by default, removed when the object is destroyed.

The file 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(); a relative template is resolved against the current directory.

QTemporaryFile file;
if (file.open()) {
    file.write("scratch data");
    const QString name = file.fileName();
}

See also QTemporaryDir, QFile and QSaveFile.

Differences from Qt

open() always opens the file for reading and writing, whatever mode was requested; only the Append, Truncate, Text and Unbuffered flags are honored. createNativeFile() copies resources only; a file that is reached through a custom file engine does not exist in Nt.

On Zephyr the default directory, /tmp, does not exist, so give a template with an absolute path on a mounted file system. See Nt on Zephyr.

Member Function Documentation

QTemporaryFile::QTemporaryFile()

Constructs a QTemporaryFile using the default template, which is qt_temp.XXXXXX in QDir::tempPath(). The file is created by open().

See also setFileTemplate().

[explicit] QTemporaryFile::QTemporaryFile(const QString &templateName)

Constructs a QTemporaryFile with the given templateName. The file is created by open().

See also setFileTemplate().

[explicit] QTemporaryFile::QTemporaryFile(QObject *parent)

Constructs a QTemporaryFile with the given parent, using the default template.

QTemporaryFile::QTemporaryFile(const QString &templateName, QObject *parent)

Constructs a QTemporaryFile with the given parent, using templateName as the file name template.

[override] QTemporaryFile::~QTemporaryFile()

Destroys the temporary file object, closing the file. The file is deleted if autoRemove() is true.

See also autoRemove().

bool QTemporaryFile::autoRemove() const

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

See also setAutoRemove().

[static] QTemporaryFile * QTemporaryFile::createNativeFile(QFile &file)

Returns a temporary copy of file if it is a resource, for code that needs a file on disk; returns nullptr if file is a native file already, has no name or cannot be read. The copy is open for reading and writing at position 0, and the caller owns it. file is left as it was: open at the same position, or closed.

QFile icon(":/icons/open.svg");
std::unique_ptr<QTemporaryFile> copy(QTemporaryFile::createNativeFile(icon));
const QString path = copy ? copy->fileName() : icon.fileName();

See also QFileInfo::isNativePath().

[static] QTemporaryFile * QTemporaryFile::createNativeFile(const QString &fileName)

This is an overloaded function.

Returns a temporary copy of the file fileName if it is a resource, or nullptr if it is a native file already.

QString QTemporaryFile::fileName() const

Returns the name of the temporary file, or an empty string if it has not been created yet.

See also QFileDevice::fileName().

QString QTemporaryFile::fileTemplate() const

Returns the file name template, which is qt_temp.XXXXXX in QDir::tempPath() by default.

See also setFileTemplate().

bool QTemporaryFile::open()

This is an overloaded function.

Creates the file from the template and opens it for reading and writing. Returns true on success; otherwise returns false.

[override, protected] bool QTemporaryFile::open(OpenMode mode)

Reimplements QFile::open().

Creates the file from the template and opens it for reading and writing. Only the Append, Truncate, Text and Unbuffered flags of mode are used. Returns true on success; otherwise returns false and sets error() to OpenError. If the file was already created by an earlier call, it is reopened under the same name.

See also open().

bool QTemporaryFile::rename(const QString &newName)

Renames the temporary file to newName, keeping it open, and returns true if successful. Unlike QFile::rename(), it never copies the file, so it fails if newName is on another file system. Whether the file is removed on destruction still depends on autoRemove().

See also setAutoRemove().

void QTemporaryFile::setAutoRemove(bool b)

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

See also autoRemove().

void QTemporaryFile::setFileTemplate(const QString &name)

Sets the file name template to name. If the file is already created, the template has no effect.

See also fileTemplate().