QSaveFile 类提供安全写入文件的接口。 更多...
头: | #include <QSaveFile> |
CMake: |
find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Since: | Qt 5.1 |
继承: | QFileDevice |
注意: 此类的所有函数 可重入 .
QSaveFile (const QString & name , QObject * parent ) | |
QSaveFile (QObject * parent = nullptr) | |
QSaveFile (const QString & name ) | |
virtual | ~QSaveFile () |
void | cancelWriting () |
bool | commit () |
bool | directWriteFallback () const |
void | setDirectWriteFallback (bool enabled ) |
void | setFileName (const QString & name ) |
virtual QString | fileName () const override |
virtual bool | open (QIODeviceBase::OpenMode mode ) override |
virtual qint64 | writeData (const char * data , qint64 len ) override |
QSaveFile 是用于写入文本和二进制文件的 I/O 设备,不会丢失现有数据,若写入操作失败。
While writing, the contents will be written to a temporary file, and if no error happened, commit () will move it to the final file. This ensures that no data at the final file is lost in case an error happens while writing, and no partially-written file is ever present at the final location. Always use QSaveFile when saving entire documents to disk.
QSaveFile automatically detects errors while writing, such as the full partition situation, where write () cannot write all the bytes. It will remember that an error happened, and will discard the temporary file in commit ().
很像 QFile ,文件的打开是采用 open ()。数据的读写通常是使用 QDataStream or QTextStream ,但也可以调用 QIODevice 继承函数 read (), readLine (), readAll (), write ().
不像 QFile ,调用 close () 不允许。 commit () 会替换它。若 commit () 未被调用且 QSaveFile 实例被销毁,丢弃临时文件。
要中止由于应用程序出错的保存,调用 cancelWriting (),所以,即使调用 commit () 稍后也不会保存。
另请参阅 QTextStream , QDataStream , QFileInfo , QDir , QFile ,和 QTemporaryFile .
构造新文件对象采用给定 parent 表示文件采用指定 name .
构造新文件对象采用给定 parent .
构造新文件对象以表示文件采用给定 name .
[虚拟]
QSaveFile::
~QSaveFile
()
销毁文件对象,丢弃保存内容除非 commit () 被调用。
取消写入新文件。
If the application changes its mind while saving, it can call cancelWriting(), which sets an error code so that commit () will discard the temporary file.
Alternatively, it can simply make sure not to call commit ().
Further write operations are possible after calling this method, but none of it will have any effect, the written file will be discarded.
This method has no effect when direct write fallback is used. This is the case when saving over an existing file in a readonly directory: no temporary file can be created, so the existing file is overwritten no matter what, and cancelWriting() cannot do anything about that, the contents of the existing file will be lost.
另请参阅 commit ().
把改变提交到磁盘,若先前的所有写入成功。
It is mandatory to call this at the end of the saving operation, otherwise the file will be discarded.
If an error happened during writing, deletes the temporary file and returns
false
. Otherwise, renames it to the final
fileName
并返回
true
on success. Finally, closes the device.
另请参阅 cancelWriting ().
返回
true
若在只读目录下保存文件的回退解决方案被启用。
另请参阅 setDirectWriteFallback ().
[override virtual]
QString
QSaveFile::
fileName
() const
重实现: QFileDevice::fileName () const.
返回名称设置通过 setFileName () 或到 QSaveFile 构造函数。
另请参阅 setFileName ().
[override virtual]
bool
QSaveFile::
open
(
QIODeviceBase::OpenMode
mode
)
重实现: QIODevice::open (QIODeviceBase::OpenMode mode).
打开文件使用 OpenMode mode ,返回 true,若成功;否则返回 false。
重要: mode 必须包括 QIODevice::WriteOnly。它还可以拥有额外标志,譬如 QIODevice::Text 和 QIODevice::Unbuffered。
QIODevice::ReadWrite、QIODevice::Append、QIODevice::NewOnly 及 QIODevice::ExistingOnly 目前不被支持。
另请参阅 QIODevice::OpenMode and setFileName ().
允许写入现有文件,若有必要。
QSaveFile creates a temporary file in the same directory as the final file and atomically renames it. However this is not possible if the directory permissions do not allow creating new files. In order to preserve atomicity guarantees, open () fails when it cannot create the temporary file.
In order to allow users to edit files with write permissions in a directory with restricted permissions, call setDirectWriteFallback() with enabled set to true, and the following calls to open () will fallback to opening the existing file directly and writing into it, without the use of a temporary file. This does not have atomicity guarantees, i.e. an application crash or for instance a power failure could lead to a partially-written file on disk. It also means cancelWriting () has no effect, in such a case.
Typically, to save documents edited by the user, call setDirectWriteFallback(true), and to save application internal files (configuration files, data files, ...), keep the default setting which ensures atomicity.
另请参阅 directWriteFallback ().
设置 name 为文件。名称可以没有路径、相对路径或绝对路径。
另请参阅 QFile::setFileName () 和 fileName ().
[override virtual protected]
qint64
QSaveFile::
writeData
(const
char
*
data
,
qint64
len
)
重实现: QFileDevice::writeData (const char *data, qint64 len).