QgsVectorLayerExporterTask class

QgsTask task which performs a QgsVectorLayerExporter layer export operation as a background task.

This can be used to export a vector layer out to a provider without blocking the QGIS interface.

Base classes

class QgsTask
Abstract base class for long running background tasks.

Public static functions

static auto withLayerOwnership(QgsVectorLayer* layer, const QString& uri, const QString& providerKey, const QgsCoordinateReferenceSystem& destinationCrs, const QMap<QString, QVariant>& options = QMap<QString, QVariant>()) -> QgsVectorLayerExporterTask*
Creates a new QgsVectorLayerExporterTask which has ownership over a source layer.

Constructors, destructors, conversion operators

QgsVectorLayerExporterTask(QgsVectorLayer* layer, const QString& uri, const QString& providerKey, const QgsCoordinateReferenceSystem& destinationCrs, const QMap<QString, QVariant>& options = QMap<QString, QVariant>(), bool ownsLayer = false)
Constructor for QgsVectorLayerExporterTask.

Public functions

void cancel() override
Notifies the task that it should terminate.

Signals

void errorOccurred(int error, const QString& errorMessage)
Emitted when an error occurs which prevented the layer being exported (or if the task is canceled).
void exportComplete()
Emitted when exporting 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

static QgsVectorLayerExporterTask* QgsVectorLayerExporterTask::withLayerOwnership(QgsVectorLayer* layer, const QString& uri, const QString& providerKey, const QgsCoordinateReferenceSystem& destinationCrs, const QMap<QString, QVariant>& options = QMap<QString, QVariant>())

Creates a new QgsVectorLayerExporterTask which has ownership over a source layer.

When the export task has completed (successfully or otherwise) layer will be deleted. The destination uri and providerKey, and various export related parameters such as destination CRS and export options must be specified.

QgsVectorLayerExporterTask::QgsVectorLayerExporterTask(QgsVectorLayer* layer, const QString& uri, const QString& providerKey, const QgsCoordinateReferenceSystem& destinationCrs, const QMap<QString, QVariant>& options = QMap<QString, QVariant>(), bool ownsLayer = false)

Constructor for QgsVectorLayerExporterTask.

Takes a source layer, destination uri and providerKey, and various export related parameters such as destination CRS and export options. ownsLayer has to be set to true if the task should take ownership of the layer and delete it after export.

void QgsVectorLayerExporterTask::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 QgsVectorLayerExporterTask::errorOccurred(int error, const QString& errorMessage) signal

Emitted when an error occurs which prevented the layer being exported (or if the task is canceled).

The export error and errorMessage will be reported.

void QgsVectorLayerExporterTask::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 QgsVectorLayerExporterTask::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.