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() 返回的应用程序名称(否则为qt_temp
)为模板,构造一个 QTemporaryDir。该目录存储在系统临时目录QDir::tempPath() 中。
另请参见 QDir::tempPath()。
[explicit]
QTemporaryDir::QTemporaryDir(const QString &templatePath)
构造一个模板为templatePath 的 QTemporaryDir。
如果templatePath 是相对路径,路径将是相对于当前工作目录的。如果想使用系统临时目录,可以使用QDir::tempPath() 构造templatePath 。
如果templatePath 以 XXXXXX 结尾,它将被用作目录名的动态部分,否则将被附加。与QTemporaryFile 不同,不支持模板字符串中间的 XXXXXX。
另请参阅 QDir::tempPath() 。
[noexcept, since 6.4]
QTemporaryDir::QTemporaryDir(QTemporaryDir &&other)
移动-从other 构建一个新的 QTemporaryDir。
注意: moved-from 对象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 实例。
注意: moved-from 对象other 处于部分形成状态,在这种状态下,唯一有效的操作是销毁和赋新值。
此函数在 Qt 6.4 中引入。
© 2025 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.