QTemporaryDir Class

QTemporaryDir class creates a unique directory for temporary use. 更多...

头: #include <QTemporaryDir>
qmake: QT += core

注意: 此类的所有函数 可重入 .

公共函数

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

详细描述

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 的构造函数。

范例:

    // Within a function/method...
    QTemporaryDir dir;
    if (dir.isValid()) {
        // dir.path() returns the unique directory path
    }
    // The QTemporaryDir destructor removes the temporary directory
    // as it goes out of scope.
					

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 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.

另请参阅 QDir::tempPath (), QDir ,和 QTemporaryFile .

成员函数文档编制

QTemporaryDir:: QTemporaryDir ()

构造 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 ().

另请参阅 QDir::tempPath ().

QTemporaryDir:: QTemporaryDir (const QString & templatePath )

构造 QTemporaryDir with a template of templatePath .

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.

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.

另请参阅 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.

另请参阅 autoRemove ().

bool QTemporaryDir:: autoRemove () const

返回 true 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.

另请参阅 setAutoRemove () 和 remove ().

QString QTemporaryDir:: errorString () const

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

该函数在 Qt 5.6 引入。

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

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.

该函数在 Qt 5.9 引入。

bool QTemporaryDir:: isValid () const

返回 true QTemporaryDir was created successfully.

QString QTemporaryDir:: path () const

Returns the path to the temporary directory. Empty if the QTemporaryDir could not be created.

bool QTemporaryDir:: remove ()

Removes the temporary directory, including all its contents.

返回 true if removing was successful.

void QTemporaryDir:: setAutoRemove ( bool b )

设置 QTemporaryDir into auto-remove mode if b 为 true。

Auto-remove is on by default.

另请参阅 autoRemove () 和 remove ().