QgsVectorFileWriterTask class
QgsTask task which performs a QgsVectorFileWriter layer saving operation as a background task.
Contents
This can be used to save a vector layer out to a file without blocking the QGIS interface.
Base classes
- class QgsTask
- Abstract base class for long running background tasks.
Constructors, destructors, conversion operators
-
QgsVectorFileWriterTask(QgsVectorLayer* layer,
const QString& fileName,
const QgsVectorFileWriter::
SaveVectorOptions& options) - Constructor for QgsVectorFileWriterTask.
Public functions
- void cancel() override
- Notifies the task that it should terminate.
Signals
- void completed(const QString& newFilename, const QString& newLayer)
- Emitted when writing the layer is successfully completed.
- void errorOccurred(int error, const QString& errorMessage)
- Emitted when an error occurs which prevented the file being written (or if the task is canceled).
- void writeComplete(const QString& newFilename)
- Emitted when writing the layer is successfully completed.
Protected functions
- void finished(bool result) override
- If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether through successful completion or via early termination).
- auto run() -> bool override
- Performs the task's operation.
Function documentation
QgsVectorFileWriterTask:: QgsVectorFileWriterTask(QgsVectorLayer* layer,
const QString& fileName,
const QgsVectorFileWriter:: SaveVectorOptions& options)
Constructor for QgsVectorFileWriterTask.
Takes a source layer, destination fileName and save options.
void QgsVectorFileWriterTask:: cancel() override
Notifies the task that it should terminate.
Calling this is not guaranteed to immediately end the task, rather it sets the isCanceled() flag which task subclasses can check and terminate their operations at an appropriate time. Any subtasks owned by this task will also be canceled. Derived classes must ensure that the base class implementation is called from any overridden version.
void QgsVectorFileWriterTask:: completed(const QString& newFilename,
const QString& newLayer) signal
Emitted when writing the layer is successfully completed.
The newFilename parameter indicates the file path for the written file. When applicable, the newLayer parameter indicates the layer name used.
void QgsVectorFileWriterTask:: errorOccurred(int error,
const QString& errorMessage) signal
Emitted when an error occurs which prevented the file being written (or if the task is canceled).
The writing error and errorMessage will be reported.
void QgsVectorFileWriterTask:: writeComplete(const QString& newFilename) signal
Emitted when writing the layer is successfully completed.
The newFilename parameter indicates the file path for the written file.
void QgsVectorFileWriterTask:: finished(bool result) override protected
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether through successful completion or via early termination).
The result argument reflects whether the task was successfully completed or not. This method is always called from the main thread, so it is safe to create widgets and perform other operations which require the main thread. However, the GUI will be blocked for the duration of this method so tasks should avoid performing any lengthy operations here.
bool QgsVectorFileWriterTask:: run() override protected
Performs the task's operation.
This method will be called when the task commences (ie via calling start() ), and subclasses should implement the operation they wish to perform in the background within this method.
A task must return a boolean value to indicate whether the task was completed successfully or terminated before completion.