QTemporaryDir Class
QTemporaryDir 类用于创建一个仅供临时使用的唯一目录。更多内容...
| 头文件: | #include <QTemporaryDir> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
- 所有成员的列表,包括继承的成员
- QTemporaryDir 属于“输入/输出与网络”模块。
注意:该类中的所有函数均为可重入函数。
公共函数
| QTemporaryDir() | |
| QTemporaryDir(const QString &templatePath) | |
(since 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) |
(since 6.4) void | swap(QTemporaryDir &other) |
(since 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() 获取临时目录的路径。
临时目录的名称包含静态部分和经过计算以确保唯一的动态部分。默认路径将由QCoreApplication::applicationName()(否则为qt_temp )确定,并作为QDir::tempPath() 返回的临时路径的一部分。 如果您指定了自己的路径,默认情况下,该路径不会被视为临时目录内的路径,而是相对于当前工作目录的相对路径。在所有情况下,都会在路径后附加一个随机字符串,以确保其唯一性。
另请参阅 QDir::tempPath()、QDir 以及QTemporaryFile 。
成员函数文档
QTemporaryDir::QTemporaryDir()
使用QCoreApplication::applicationName()返回的应用程序名称作为模板构建一个QTemporaryDir(否则使用qt_temp )。该目录存储在系统的临时目录QDir::tempPath()中。
另请参阅 QDir::tempPath()。
[explicit] QTemporaryDir::QTemporaryDir(const QString &templatePath)
使用模板 `templatePath` 构建一个 `QTemporaryDir`。
如果 `templatePath ` 是相对路径,则该路径将相对于当前工作目录。若要使用系统的临时目录,可以使用 `QDir::tempPath()` 来创建 `templatePath `。
如果templatePath 以 XXXXXX 结尾,则将其用作目录名的动态部分;否则,XXXXXX 将被追加到目录名末尾。与QTemporaryFile 不同,模板字符串中间的 XXXXXX 不受支持。
另请参阅 QDir::tempPath()。
[noexcept, since 6.4] QTemporaryDir::QTemporaryDir(QTemporaryDir &&other)
从 `other` 通过移动操作构造一个新的 `QTemporaryDir`。
注意: 被移动的对象 other 将处于部分初始化的状态,在此状态下,唯一有效的操作是销毁和赋值。
该函数在 Qt 6.4 中引入。
[noexcept] QTemporaryDir::~QTemporaryDir()
销毁临时目录对象。如果设置了自动删除模式,则会自动删除该目录及其所有内容。
另请参阅 autoRemove()。
bool QTemporaryDir::autoRemove() const
如果 `QTemporaryDir ` 处于自动删除模式,则返回 `true `。自动删除模式会在对象销毁时自动从磁盘中删除该目录。这使得在栈上创建 `QTemporaryDir ` 对象、向其中添加文件、对文件进行操作,最后在函数返回时由其自动清理变得非常简单。
自动删除功能默认开启。
另请参阅 setAutoRemove() 和remove()。
QString QTemporaryDir::errorString() const
如果 `isValid()` 返回 `false`,则该函数将返回一个错误字符串,说明临时目录创建失败的原因。否则,该函数返回一个空字符串。
QString QTemporaryDir::filePath(const QString &fileName) const
返回临时目录中某个文件的路径名。不会检查该文件是否实际存在于该目录中。fileName 中多余的分隔符或“.”和“..”目录不会被移除(参见QDir::cleanPath())。不允许使用绝对路径。
返回的路径是相对路径还是绝对路径,取决于QTemporaryDir 在构建时分别使用了相对路径还是绝对路径。
bool QTemporaryDir::isValid() const
如果QTemporaryDir 已成功创建,则返回true 。
QString QTemporaryDir::path() const
返回临时目录的路径。如果无法创建QTemporaryDir ,则返回空值。
返回的路径是相对路径还是绝对路径,取决于QTemporaryDir 在构建时分别使用了相对路径还是绝对路径。
bool QTemporaryDir::remove()
删除临时目录及其所有内容。
如果删除成功,则返回true 。
void QTemporaryDir::setAutoRemove(bool b)
如果 `b ` 为 `true`,则将 `QTemporaryDir ` 设置为自动移除模式。
自动移除功能默认处于启用状态。
另请参阅 autoRemove() 和remove()。
[noexcept, since 6.4] void QTemporaryDir::swap(QTemporaryDir &other)
将该临时目录与other 互换。此操作速度极快,且绝不会失败。
该函数于 Qt 6.4 中引入。
[noexcept, since 6.4] QTemporaryDir &QTemporaryDir::operator=(QTemporaryDir &&other)
将other 通过移动赋值操作赋值给此QTemporaryDir 实例。
注意: 被移动的对象 other 将处于部分初始化状态,在此状态下,唯一有效的操作是销毁和赋值。
该函数于 Qt 6.4 中引入。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.