CopperSpice API  1.9.1
QTemporaryDir Class Reference

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

Public Methods

 QTemporaryDir ()
 
 QTemporaryDir (const QString &tempPath)
 
 ~QTemporaryDir ()
 
bool autoRemove () const
 
QString errorString () const
 
bool isValid () const
 
QString path () const
 
bool remove ()
 
void setAutoRemove (bool b)
 

Detailed Description

The QTemporaryDir class creates a unique directory for temporary use.

QTemporaryDir is used to create unique temporary directories without a race condition. The directory is created by the constructor. The name of the temporary directory is guaranteed to be unique. It will be removed when QTemporaryDir object is destroyed. The directory name is either auto generated or created based on a template.

if (dir.isValid()) {
// dir.path() returns the unique directory path
}

It is very important to test that the temporary directory could be created, using isValid(). Do not use 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 cs_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

Constructor & Destructor Documentation

QTemporaryDir::QTemporaryDir ( )

Constructs a QTemporaryDir using as template the application name returned by QCoreApplication::applicationName() (otherwise cs_temp). The directory is stored in the system's temporary directory, QDir::tempPath().

See also
QDir::tempPath()
QTemporaryDir::QTemporaryDir ( const QString tempPath)
explicit

Constructs a QTemporaryDir with a template of tempPath.

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

If the tempPath 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()
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()

Method Documentation

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()
QString QTemporaryDir::errorString ( ) const

If calling isValid() would return false then this method returns an error string explaining why the creation of the temporary directory failed. Otherwise, returns an empty string.

bool QTemporaryDir::isValid ( ) const

Returns true if the QTemporaryDir was created successfully.

QString QTemporaryDir::path ( ) const

Returns the path to the temporary directory. Will be an empty string if QTemporaryDir did not create a directory.

bool QTemporaryDir::remove ( )

Removes the temporary directory, including all its contents. Returns true if removing was successful.

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()