QTemporaryFile 类是操作临时文件的 I/O 设备。 更多...
| 头: |
#include <QTemporaryFile>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
| 继承: | QFile |
注意: 此类的所有函数 可重入 .
| QTemporaryFile () | |
| QTemporaryFile (QObject * parent ) | |
| QTemporaryFile (const QString & templateName ) | |
| QTemporaryFile (const QString & templateName , QObject * parent ) | |
(从 6.7 起)
|
QTemporaryFile (const std::filesystem::path & templateName , QObject * parent = nullptr) |
| virtual | ~QTemporaryFile () |
| bool | autoRemove () const |
| QString | fileTemplate () const |
| bool | open () |
| bool | rename (const QString & newName ) |
(从 6.7 起)
bool
|
rename (const std::filesystem::path & newName ) |
| void | setAutoRemove (bool b ) |
| void | setFileTemplate (const QString & templateName ) |
(从 6.7 起)
void
|
setFileTemplate (const std::filesystem::path & name ) |
| virtual QString | fileName () const override |
| QTemporaryFile * | createNativeFile (QFile & file ) |
| QTemporaryFile * | createNativeFile (const QString & fileName ) |
(从 6.7 起)
QTemporaryFile *
|
createNativeFile (const std::filesystem::path & fileName ) |
| virtual bool | open (QIODeviceBase::OpenMode mode ) override |
QTemporaryFile 用于安全创建唯一临时文件。文件本身的创建是通过调用 open (). The name of the temporary file is guaranteed to be unique (i.e., you are guaranteed to not overwrite an existing file), and the file will subsequently be removed upon destruction of the QTemporaryFile object. This is an important technique that avoids data corruption for applications that store data in temporary files. The file name is either auto-generated, or created based on a template, which is passed to QTemporaryFile's constructor.
范例:
// Within a function/method...
QTemporaryFile file;
if (file.open()) {
// file.fileName() returns the unique file name
}
// The QTemporaryFile destructor removes the temporary file
// as it goes out of scope.
Reopening a QTemporaryFile after calling close () is safe. For as long as the QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by QTemporaryFile.
The file name of the temporary file can be found by calling fileName (). Note that this is only defined after the file is first opened; the function returns an empty string before this.
A temporary file will have some static part of the name and some part that is calculated to be unique. The default filename 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 filename, a relative file path will not be placed in the temporary directory by default, but be relative to the current working directory.
It is important to specify the correct directory if the rename () function will be called, as QTemporaryFile can only rename files within the same volume / filesystem as the temporary file itself was created on.
The file name (the part after the last directory path separator in the specified file template) can contain the special sequence
"XXXXXX"
(at least six upper case
"X"
characters), which will be replaced with the auto-generated portion of the file name. If the file name doesn't contain
"XXXXXX"
, QTemporaryFile will append the generated part to the file name. Only the last occurrence of
"XXXXXX"
will be considered.
注意: On Linux, QTemporaryFile will attempt to create unnamed temporary files. If that succeeds, open () will return true but exists () will be false. If you call fileName () or any function that calls it, QTemporaryFile will give the file a name, so most applications will not see a difference.
另请参阅 QDir::tempPath () 和 QFile .
Constructs a QTemporaryFile.
The default file name template is determined from the application name as returned by
QCoreApplication::applicationName
() (或
"qt_temp"
if the application name is empty), followed by
".XXXXXX"
. The file is stored in the system's temporary directory, as returned by
QDir::tempPath
().
另请参阅 setFileTemplate (), fileTemplate (), fileName (),和 QDir::tempPath ().
[explicit]
QTemporaryFile::
QTemporaryFile
(
QObject
*
parent
)
Constructs a QTemporaryFile with the given parent .
The default file name template is determined from the application name as returned by
QCoreApplication::applicationName
() (或
"qt_temp"
if the application name is empty), followed by
".XXXXXX"
. The file is stored in the system's temporary directory, as returned by
QDir::tempPath
().
另请参阅 setFileTemplate ().
[explicit]
QTemporaryFile::
QTemporaryFile
(const
QString
&
templateName
)
Constructs a QTemporaryFile with templateName as the file name template.
Upon opening the temporary file, templateName will be used to create a unique filename.
If the file name (the part after the last directory path separator in
templateName
) doesn't contain
"XXXXXX"
, it will be added automatically.
"XXXXXX"
will be replaced with the dynamic part of the file name, which is calculated to be unique.
若 templateName is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath () to construct templateName if you want use the system's temporary directory.
It is important to specify the correct directory if the rename () function will be called, as QTemporaryFile can only rename files within the same volume / filesystem as the temporary file itself was created on.
另请参阅 open () 和 fileTemplate ().
Constructs a QTemporaryFile with the specified parent ,和 templateName as the file name template.
Upon opening the temporary file, templateName will be used to create a unique filename.
If the file name (the part after the last directory path separator in
templateName
) doesn't contain
"XXXXXX"
, it will be added automatically.
"XXXXXX"
will be replaced with the dynamic part of the file name, which is calculated to be unique.
若 templateName is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath () to construct templateName if you want use the system's temporary directory. It is important to specify the correct directory if the rename () function will be called, as QTemporaryFile can only rename files within the same volume / filesystem as the temporary file itself was created on.
另请参阅 open () 和 fileTemplate ().
[explicit, since 6.7]
QTemporaryFile::
QTemporaryFile
(const
std::filesystem::path
&
templateName
,
QObject
*
parent
= nullptr)
这是重载函数。
该函数在 Qt 6.7 引入。
[virtual noexcept]
QTemporaryFile::
~QTemporaryFile
()
Destroys the temporary file object, the file is automatically closed if necessary and if in auto remove mode it will automatically delete the file.
另请参阅 autoRemove ().
返回
true
若
QTemporaryFile
is in auto remove mode. Auto-remove mode will automatically delete the filename from disk upon destruction. This makes it very easy to create your
QTemporaryFile
object on the stack, fill it with data, read from it, and finally on function return it will automatically clean up after itself.
默认情况下,开启自动移除。
另请参阅 setAutoRemove () 和 remove ().
[static]
QTemporaryFile
*QTemporaryFile::
createNativeFile
(
QFile
&
file
)
若
file
已经不是本机文件,那么
QTemporaryFile
的创建是在
QDir::tempPath
(), the contents of
file
is copied into it, and a pointer to the temporary file is returned. Does nothing and returns
0
if
file
已是本机文件。
例如:
QFile f(":/resources/file.txt"); QTemporaryFile::createNativeFile(f); // Returns a pointer to a temporary file QFile f("/users/qt/file.txt"); QTemporaryFile::createNativeFile(f); // Returns 0
另请参阅 QFileInfo::isNativePath ().
[static]
QTemporaryFile
*QTemporaryFile::
createNativeFile
(const
QString
&
fileName
)
这是重载函数。
Works on the given fileName rather than an existing QFile 对象。
[static, since 6.7]
QTemporaryFile
*QTemporaryFile::
createNativeFile
(const
std::filesystem::path
&
fileName
)
这是重载函数。
该函数在 Qt 6.7 引入。
[override virtual]
QString
QTemporaryFile::
fileName
() const
重实现: QFile::fileName () const.
Returns the complete unique filename backing the QTemporaryFile object. This string is null before the QTemporaryFile is opened, afterwards it will contain the fileTemplate () plus additional characters to make it unique.
The file name returned by this method is relative or absolute depending on the file name template used to construct this object (or passed to setFileTemplate ()) being relative or absolute, respectively.
另请参阅 fileTemplate ().
Returns the file name template.
The file name template returned by this method, will be relative or absolute depending on the file name template used to construct this object (or passed to setFileTemplate ()) being relative or absolute, respectively.
另请参阅 setFileTemplate (), fileName (),和 Default File Name Template .
Opens a unique temporary file in the file system in
QIODeviceBase::ReadWrite
mode. Returns
true
if the file was successfully opened, or was already open. Otherwise returns
false
.
If called for the first time, open() will create a unique file name based on fileTemplate (). The file is guaranteed to have been created by this function (that is, it has never existed before).
If a file is reopened after calling close (), the same file will be opened again.
另请参阅 setFileTemplate () 和 QT_USE_NODISCARD_FILE_OPEN .
[override virtual protected]
bool
QTemporaryFile::
open
(
QIODeviceBase::OpenMode
mode
)
重实现: QFile::open (QIODeviceBase::OpenMode mode).
Opens a unique temporary file in the file system with
mode
flags. Returns
true
if the file was successfully opened, or was already open. Otherwise returns
false
.
If called for the first time, open() will create a unique file name based on fileTemplate (), and open it with mode flags. The file is guaranteed to have been created by this function (that is, it has never existed before).
If a file is reopened after calling close (), the same file will be opened again with mode flags.
另请参阅 setFileTemplate () 和 QT_USE_NODISCARD_FILE_OPEN .
把当前临时文件重命名为 newName 并返回 true 若成功。
This function has an important difference compared to QFile::rename (): it will not perform a copy+delete if the low-level system call to rename the file fails, something that could happen if newName specifies a file in a different volume or filesystem than the temporary file was created on. In other words, QTemporaryFile only supports atomic file renaming.
This functionality is intended to support materializing the destination file with all contents already present, so another process cannot see an incomplete file in the process of being written. The QSaveFile class can be used for a similar purpose too, particularly if the destination file is not temporary.
注意:
Calling rename() does not disable
autoRemove
. If you want the renamed file to persist, you must call
setAutoRemove
and set it to
false
after calling rename(). Otherwise, the file will be deleted when the
QTemporaryFile
对象被销毁。
另请参阅 QSaveFile , QSaveFile::commit (),和 QFile::rename ().
[since 6.7]
bool
QTemporaryFile::
rename
(const
std::filesystem::path
&
newName
)
这是重载函数。
该函数在 Qt 6.7 引入。
设置
QTemporaryFile
成自动移除模式若
b
is
true
.
默认情况下,开启自动移除。
若把此特性设为
false
, ensure the application provides a way to remove the file once it is no longer needed, including passing the responsibility on to another process. Always use the
fileName
() function to obtain the name and never try to guess the name that
QTemporaryFile
has generated.
On some systems, if fileName () is not called before closing the file, the temporary file may be removed regardless of the state of this property. This behavior should not be relied upon, so application code should either call fileName () or leave the auto removal functionality enabled.
另请参阅 autoRemove () 和 remove ().
Sets the file name template to templateName .
If the file name (the part after the last directory path separator in
templateName
) doesn't contain
"XXXXXX"
, it will be added automatically.
"XXXXXX"
will be replaced with the dynamic part of the file name, which is calculated to be unique.
若 templateName is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath () to construct templateName if you want use the system's temporary directory. It is important to specify the correct directory if the rename () function will be called, as QTemporaryFile can only rename files within the same volume / filesystem as the temporary file itself was created on.
另请参阅 fileTemplate () 和 fileName ().
[since 6.7]
void
QTemporaryFile::
setFileTemplate
(const
std::filesystem::path
&
name
)
这是重载函数。
该函数在 Qt 6.7 引入。