QSaveFile 類

The QSaveFile class provides an interface for safely writing to files. 更多...

頭: #include <QSaveFile>
qmake: QT += core
Since: Qt 5.1
繼承: QFileDevice

注意: 此類的所有函數 可重入 .

公共函數

QSaveFile (const QString & name )
QSaveFile (QObject * parent = Q_NULLPTR)
QSaveFile (const QString & name , QObject * parent )
~QSaveFile ()
void cancelWriting ()
bool commit ()
bool directWriteFallback () const
void setDirectWriteFallback (bool enabled )
void setFileName (const QString & name )

重實現公共函數

virtual QString fileName () const
virtual bool open (OpenMode mode )

重實現保護函數

virtual qint64 writeData (const char * data , qint64 len )

額外繼承成員

詳細描述

The QSaveFile class provides an interface for safely writing to files.

QSaveFile is an I/O device for writing text and binary files, without losing existing data if the writing operation fails.

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 () was not called and the QSaveFile instance is destroyed, the temporary file is discarded.

要中止由於應用程序齣錯的保存,調用 cancelWriting (),所以,即使調用 commit () 稍後也不會保存。

另請參閱 QTextStream , QDataStream , QFileInfo , QDir , QFile ,和 QTemporaryFile .

成員函數文檔編製

QSaveFile:: QSaveFile (const QString & name )

構造新文件對象以錶示文件采用給定 name .

QSaveFile:: QSaveFile ( QObject * parent = Q_NULLPTR)

構造新文件對象采用給定 parent .

QSaveFile:: QSaveFile (const QString & name , QObject * parent )

構造新文件對象采用給定 parent 錶示文件采用指定 name .

QSaveFile:: ~QSaveFile ()

銷毀文件對象,丟棄保存內容除非 commit () 被調用。

void QSaveFile:: cancelWriting ()

取消寫入新文件。

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 ().

bool QSaveFile:: 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 ().

bool QSaveFile:: directWriteFallback () const

返迴 true 若在隻讀目錄下保存文件的迴退解決方案被啓用。

另請參閱 setDirectWriteFallback ().

[虛擬] QString QSaveFile:: fileName () const

重實現自 QFileDevice::fileName ().

返迴的名稱設置通過 setFileName () 或到 QSaveFile 構造函數。

另請參閱 setFileName ().

[虛擬] bool QSaveFile:: open ( OpenMode mode )

重實現自 QIODevice::open ().

打開文件使用 OpenMode mode ,返迴 true,若成功;否則返迴 false。

重要: mode 必須包括 QIODevice::WriteOnly 。它還可以擁有額外標誌,譬如 QIODevice::Text and QIODevice::Unbuffered .

QIODevice::ReadWrite and QIODevice::Append 目前不被支持。

另請參閱 QIODevice::OpenMode and setFileName ().

void QSaveFile:: setDirectWriteFallback ( bool enabled )

允許寫入現有文件,若有必要。

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 ().

void QSaveFile:: setFileName (const QString & name )

設置 name 為文件。名稱可以沒有路徑、相對路徑或絕對路徑。

另請參閱 QFile::setFileName () 和 fileName ().

[virtual protected] qint64 QSaveFile:: writeData (const char * data , qint64 len )

重實現自 QIODevice::writeData ().