QTemporaryDir 类

QTemporaryDir 类创建用于临时使用的唯一目录。 更多...

头: #include <QTemporaryDir>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

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

公共函数

QTemporaryDir ()
QTemporaryDir (const QString & templatePath )
(从 6.4 起) QTemporaryDir (QTemporaryDir && other )
~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 )
(从 6.4 起) void swap (QTemporaryDir & other )
(从 6.4 起) QTemporaryDir & operator= (QTemporaryDir && other )

详细描述

QTemporaryDir 用于安全创建唯一临时目录。目录本身是由构造函数创建的。临时目录名保证唯一(即:保证不覆写现有目录),且随后会移除目录,当 QTemporaryDir 对象销毁时。目录名要么是自动生成的,要么是基于模板 (被传递给 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.
					

测试可以创建临时目录非常重要,是使用 isValid ()。不要使用 exists (),由于默认构造 QDir 表示当前存在目录。

临时目录路径的查找可以通过调用 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 () (否则 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 ()

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

另请参阅 QDir::tempPath ().

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

构造 QTemporaryDir 采用模板化的 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 ().

[noexcept, since 6.4] QTemporaryDir:: QTemporaryDir ( QTemporaryDir && other )

移动构造新 QTemporaryDir 从 other .

注意: 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.

该函数在 Qt 6.4 引入。

[noexcept] 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.

默认情况下,开启自动移除。

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

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 ())。绝对路径不允许。

The returned path will be relative or absolulte depending on whether QTemporaryDir was constructed with a relative or absolute path, respectively.

bool QTemporaryDir:: isValid () const

返回 true QTemporaryDir 创建成功。

QString QTemporaryDir:: path () const

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

The returned path will be relative or absolulte depending on whether QTemporaryDir was constructed with a relative or absolute path, respectively.

bool QTemporaryDir:: remove ()

移除临时目录,包括其下所有内容。

返回 true 若移除成功。

void QTemporaryDir:: setAutoRemove ( bool b )

设置 QTemporaryDir 成自动移除模式若 b 为 true。

默认情况下,开启自动移除。

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

[noexcept, since 6.4] void QTemporaryDir:: swap ( QTemporaryDir & other )

Swaps this temporary-dir with other 。此操作非常快且从不失败。

该函数在 Qt 6.4 引入。

[noexcept, since 6.4] QTemporaryDir &QTemporaryDir:: operator= ( QTemporaryDir && other )

移动赋值 other 到此 QTemporaryDir 实例。

注意: 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.

该函数在 Qt 6.4 引入。