![]() |
CopperSpice API
1.9.2
|
The QSaveFile class provides an interface for safely writing to files. More...
Public Methods | |
QSaveFile (const QString &name) | |
QSaveFile (const QString &name, QObject *parent) | |
QSaveFile (QObject *parent=nullptr) | |
~QSaveFile () | |
void | cancelWriting () |
bool | commit () |
bool | directWriteFallback () const |
QString | fileName () const override |
bool | open (OpenMode mode) override |
void | setDirectWriteFallback (bool enabled) |
void | setFileName (const QString &name) |
![]() | |
~QFileDevice () | |
bool | atEnd () const override |
FileError | error () const |
bool | flush () |
int | handle () const |
bool | isSequential () const override |
uchar * | map (qint64 offset, qint64 size, MemoryMapFlags flags=NoOptions) |
virtual Permissions | permissions () const |
qint64 | pos () const override |
virtual bool | resize (qint64 size) |
bool | seek (qint64 pos) override |
virtual bool | setPermissions (Permissions permissionSpec) |
qint64 | size () const override |
bool | unmap (uchar *address) |
void | unsetError () |
![]() | |
QIODevice () | |
QIODevice (QObject *parent) | |
virtual | ~QIODevice () |
virtual qint64 | bytesAvailable () const |
virtual qint64 | bytesToWrite () const |
virtual bool | canReadLine () const |
QString | errorString () const |
bool | getChar (char *c) |
bool | isOpen () const |
bool | isReadable () const |
bool | isTextModeEnabled () const |
bool | isWritable () const |
OpenMode | openMode () const |
qint64 | peek (char *data, qint64 maxSize) |
QByteArray | peek (qint64 maxSize) |
bool | putChar (char c) |
qint64 | read (char *data, qint64 maxSize) |
QByteArray | read (qint64 maxSize) |
QByteArray | readAll () |
qint64 | readLine (char *data, qint64 maxSize) |
QByteArray | readLine (qint64 maxSize=0) |
virtual bool | reset () |
void | setTextModeEnabled (bool enabled) |
void | ungetChar (char c) |
virtual bool | waitForBytesWritten (int msecs) |
virtual bool | waitForReadyRead (int msecs) |
qint64 | write (const char *data) |
qint64 | write (const char *data, qint64 maxSize) |
qint64 | write (const QByteArray &data) |
![]() | |
QObject (QObject *parent=nullptr) | |
~QObject () | |
bool | blockSignals (bool block) |
const QList< QObject * > & | children () const |
bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
bool | connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
bool | disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const |
bool | disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
bool | disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
void | dumpObjectInfo () |
void | dumpObjectTree () |
QList< QString > | dynamicPropertyNames () const |
virtual bool | event (QEvent *event) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
template<typename T > | |
T | findChild (const QString &childName=QString ()) const |
template<class T > | |
QList< T > | findChildren (const QRegularExpression ®Exp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
template<class T > | |
QList< T > | findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
bool | inherits (const QString &className) const |
void | installEventFilter (QObject *filterObj) |
bool | isWidgetType () const |
bool | isWindowType () const |
void | killTimer (int id) |
const QMetaObject * | metaObject () const |
void | moveToThread (QThread *targetThread) |
QString | objectName () const |
QObject * | parent () const |
template<class T = QVariant> | |
T | property (const QString &name) const |
void | removeEventFilter (QObject *obj) |
void | setObjectName (const QString &name) |
void | setParent (QObject *parent) |
bool | setProperty (const QString &name, const QVariant &value) |
bool | signalsBlocked () const |
int | startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer) |
QThread * | thread () const |
Protected Methods | |
qint64 | writeData (const char *data, qint64 len) override |
![]() | |
qint64 | readData (char *data, qint64 maxSize) override |
qint64 | readLineData (char *data, qint64 maxSize) override |
![]() | |
void | setErrorString (const QString &errorString) |
void | setOpenMode (OpenMode openMode) |
![]() | |
virtual void | childEvent (QChildEvent *event) |
virtual void | connectNotify (const QMetaMethod &signalMethod) const |
virtual void | customEvent (QEvent *event) |
virtual void | disconnectNotify (const QMetaMethod &signalMethod) const |
bool | isSignalConnected (const QMetaMethod &signalMethod) const |
int | receivers (const QString &signal) const |
QObject * | sender () const |
int | senderSignalIndex () const |
virtual void | timerEvent (QTimerEvent *event) |
Additional Inherited Members | |
![]() | |
using | FileHandleFlags = QFlags< FileHandleFlag > |
using | Permissions = QFlags< Permission > |
![]() | |
using | OpenMode = QFlags< OpenModeFlag > |
![]() | |
enum | FileError |
enum | FileHandleFlag |
enum | MemoryMapFlags |
enum | Permission |
![]() | |
enum | OpenModeFlag |
![]() | |
void | aboutToClose () |
void | bytesWritten (qint64 bytes) |
void | readChannelFinished () |
void | readyRead () |
![]() | |
void | destroyed (QObject *obj=nullptr) |
void | objectNameChanged (const QString &objectName) |
![]() | |
void | deleteLater () |
![]() | |
static bool | connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ()) |
static bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > | |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > | |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod) |
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod) |
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod) |
static bool | disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...)) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod) |
static QMetaObject & | staticMetaObject () |
static QString | tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >()) |
![]() | |
objectName | |
![]() | |
T | qobject_cast (QObject *object) |
QObjectList | |
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.
The contents will be written to a temporary file. If no error occurs commit() will rename the temporary file to the final name. 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() can not write all the bytes. It will remember that an error happened, and will discard the temporary file in commit().
Much like QFile, the file is opened with open(). Data is usually read and written using QDataStream or QTextStream, you can also call the QIODevice methods read(), readLine(), readAll(), and write(). Unlike QFile calling close() is not allowed. commit() replaces it. If commit() was not called and the QSaveFile instance is destroyed, the temporary file is discarded.
To abort saving due to an application error, call cancelWriting() so that even a call to commit() later on will not save.
|
explicit |
Constructs a new file object to represent the file with the given name.
|
explicit |
Constructs a new file object with the given parent.
Constructs a new file object with the given parent to represent the file with the specified name.
QSaveFile::~QSaveFile | ( | ) |
Destroys the file object discarding the saved contents unless commit() was called.
void QSaveFile::cancelWriting | ( | ) |
Cancels writing the new file. 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() can not do anything about that, the contents of the existing file will be lost.
bool QSaveFile::commit | ( | ) |
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 and returns true
on success. Finally, closes the device.
bool QSaveFile::directWriteFallback | ( | ) | const |
Returns true if the fallback solution for saving files in read only directories is enabled.
|
overridevirtual |
Returns the name set by setFileName() or by the QSaveFile constructor.
Reimplemented from QFileDevice::fileName()
|
overridevirtual |
Opens the file using OpenMode mode, returning true if successful, otherwise false. The mode must include QIODevice::WriteOnly. It may also have additional flags, such as QIODevice::Text and QIODevice::Unbuffered.
QIODevice::ReadWrite and QIODevice::Append are not supported at the moment.
Reimplemented from QIODevice::open()
void QSaveFile::setDirectWriteFallback | ( | bool | enabled | ) |
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 can not 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, etc), keep the default setting which ensures atomicity.
void QSaveFile::setFileName | ( | const QString & | name | ) |
Sets the name of the file. The name can have no path, a relative path, or an absolute path.
Reimplemented from QFileDevice::writeData()