def
cancelWriting
()
def
commit
()
def
directWriteFallback
()
def
setDirectWriteFallback
(enabled)
def
setFileName
(name)
QSaveFileis 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 useQSaveFilewhen saving entire documents to disk.
QSaveFileautomatically detects errors while writing, such as the full partition situation, wherewrite()cannot write all the bytes. It will remember that an error happened, and will discard the temporary file incommit().Much like with
QFile, the file is opened withopen(). Data is usually read and written usingQDataStreamorQTextStream, but you can also call theQIODevice-inherited functionsread(),readLine(),readAll(),write().不像
QFile,调用close()is not allowed.commit()replaces it. Ifcommit()was not called and theQSaveFileinstance is destroyed, the temporary file is discarded.To abort saving due to an application error, call
cancelWriting(), so that even a call tocommit()later on will not save.
QSaveFile
(
[
parent=None
]
)
¶
QSaveFile(name)
QSaveFile(name, parent)
- param parent
- param name
unicode
构造新文件对象采用给定
parent
.
构造新文件对象采用给定
parent
to represent the file with the specified
name
.
PySide2.QtCore.QSaveFile.
cancelWriting
(
)
¶
取消写入新文件。
If the application changes its mind while saving, it can call , 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 cannot do anything about that, the contents of the existing file will be lost.
另请参阅
PySide2.QtCore.QSaveFile.
commit
(
)
¶
bool
Commits the changes to disk, if all previous writes were successful.
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.
另请参阅
PySide2.QtCore.QSaveFile.
directWriteFallback
(
)
¶
bool
返回
true
若在只读目录下保存文件的回退解决方案被启用。
PySide2.QtCore.QSaveFile.
setDirectWriteFallback
(
enabled
)
¶
enabled
–
bool
Allows writing over the existing file if necessary.
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 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 (true), and to save application internal files (configuration files, data files, …), keep the default setting which ensures atomicity.
PySide2.QtCore.QSaveFile.
setFileName
(
name
)
¶
name – unicode
设置
name
of the file. The name can have no path, a relative path, or an absolute path.
另请参阅
setFileName()
fileName()