Class: QgsVectorLayerTools

Used to handle basic editing operations on vector layers.

With an implementation of this class, parts of the application can ask for an operation to be done and the implementation will then take care of it.

Reimplement this class, if you need to have custom checks or GUI elements in your application.

Note

This is an abstract class, with methods which must be implemented by a subclass.

The following methods must be implemented: saveEdits(), startEditing(), stopEditing()

Class Hierarchy

Inheritance diagram of qgis.core.QgsVectorLayerTools

Base classes

QObject

Subclasses

QgsTrackedVectorLayerTools

Vector layer tools subclass which “tracks” edits.

Abstract Methods

saveEdits

Should be called, when the features should be committed but the editing session is not ended.

startEditing

This will be called, whenever a vector layer should be switched to edit mode.

stopEditing

Will be called, when an editing session is ended and the features should be committed.

Methods

forceSuppressFormPopup

Returns force suppress form popup status.

project

Returns the project to be used by operations when needed.

setForceSuppressFormPopup

Sets force suppress form popup status to forceSuppressFormPopup.

setProject

Sets the project to be used by operations when needed.

Virtual Methods

In PyQGIS, only methods marked as virtual can be safely overridden in a Python subclass of QgsVectorLayerTools. See the FAQ for more details.

addFeature

This method should/will be called, whenever a new feature will be added to the layer

addFeatureV2

This method should/will be called, whenever a new feature will be added to the layer

copyMoveFeatures

Copy and move features with defined translation.

class qgis.core.QgsVectorLayerTools[source]

Bases: QObject

virtual addFeature(self, layer: QgsVectorLayer | None, defaultValues: Dict[int, Any] = {}, defaultGeometry: QgsGeometry = QgsGeometry(), parentWidget: QWidget | None = None, showModal: bool = True, hideParent: bool = False)[source]

This method should/will be called, whenever a new feature will be added to the layer

Parameters:
  • layer (Optional[QgsVectorLayer]) – The layer to which the feature should be added

  • defaultValues (Dict[int, Any] = {}) – Default values for the feature to add

  • defaultGeometry (QgsGeometry = QgsGeometry()) – A default geometry to add to the feature

  • parentWidget (Optional[QWidget] = None) – The widget calling this function to be passed to the used dialog

  • showModal (bool = True) – If the used dialog should be modal or not

  • hideParent (bool = False) -> (bool) – If the parent widget should be hidden, when the used dialog is opened

Returns:

  • True in case of success, False if the operation failed/was aborted

  • feature: Updated feature after adding will be written back to this

Note

addFeature or addFeatureV2 must be overwritten when implementing a class inheriting from QgsVectorLayerTools

virtual addFeatureV2(self, layer: QgsVectorLayer | None, defaultValues: Dict[int, Any] = {}, defaultGeometry: QgsGeometry = QgsGeometry(), context: QgsVectorLayerToolsContext = QgsVectorLayerToolsContext())[source]

This method should/will be called, whenever a new feature will be added to the layer

Parameters:
  • layer (Optional[QgsVectorLayer]) – The layer to which the feature should be added

  • defaultValues (Dict[int, Any] = {}) – Default values for the feature to add

  • defaultGeometry (QgsGeometry = QgsGeometry()) – A default geometry to add to the feature

  • context (QgsVectorLayerToolsContext = QgsVectorLayerToolsContext()) -> (bool) – A context object to be used for e.g. to calculate feature expression-based values (since QGIS 3.38)

Returns:

  • True in case of success, False if the operation failed/was aborted

  • feature: Updated feature after adding will be written back to this

Note

addFeature or addFeatureV2 must be overwritten when implementing a class inheriting from QgsVectorLayerTools

Added in version 3.38.

virtual copyMoveFeatures(self, layer: QgsVectorLayer | None, request: QgsFeatureRequest, dx: float = 0, dy: float = 0, topologicalEditing: bool = False, topologicalLayer: QgsVectorLayer | None = None, childrenInfoMsg: str | None | None = '')[source]

Copy and move features with defined translation.

Parameters:
  • layer (Optional[QgsVectorLayer]) – The layer

  • request (QgsFeatureRequest) – The request for the features to be moved. It will be assigned to a new feature request with the newly copied features.

  • dx (float = 0) – The translation on x

  • dy (float = 0) – The translation on y

  • topologicalEditing (bool = False) – If True, the function will perform topological editing of the vertices of layer on layer and topologicalLayer

  • topologicalLayer (Optional[QgsVectorLayer] = None) – The layer where vertices from the moved features of layer will be added

  • childrenInfoMsg (Optional[Optional[str]] = '') -> (bool) – If given, it will contain messages related to the creation of child features

Returns:

  • True if all features could be copied.

  • errorMsg: If given, it will contain the error message

forceSuppressFormPopup(self) bool[source]

Returns force suppress form popup status.

Return type:

bool

Returns:

True if force suppress form popup is set.

Added in version 3.14.

project(self) QgsProject | None[source]

Returns the project to be used by operations when needed.

Added in version 3.34.

Return type:

Optional[QgsProject]

abstract saveEdits(self, layer: QgsVectorLayer | None) bool[source]

Should be called, when the features should be committed but the editing session is not ended.

Parameters:

layer (Optional[QgsVectorLayer]) – The layer to commit

Return type:

bool

Returns:

True if successful

setForceSuppressFormPopup(self, forceSuppressFormPopup: bool)[source]

Sets force suppress form popup status to forceSuppressFormPopup.

This flag will override the layer and general settings regarding the automatic opening of the attribute form dialog when digitizing is completed.

Added in version 3.14.

Parameters:

forceSuppressFormPopup (bool)

setProject(self, project: QgsProject | None)[source]

Sets the project to be used by operations when needed.

Added in version 3.34.

Parameters:

project (Optional[QgsProject])

abstract startEditing(self, layer: QgsVectorLayer | None) bool[source]

This will be called, whenever a vector layer should be switched to edit mode. Check the providers capability to edit in here. If successful layer->:py:func:~QgsVectorLayerTools.startEditing will be called and True returned.

Parameters:

layer (Optional[QgsVectorLayer]) – The layer on which to start an edit session

Return type:

bool

Returns:

True, if the editing session was started

abstract stopEditing(self, layer: QgsVectorLayer | None, allowCancel: bool = True) bool[source]

Will be called, when an editing session is ended and the features should be committed. Appropriate dialogs should be shown like

Parameters:
  • layer (Optional[QgsVectorLayer]) – The layer to commit

  • allowCancel (bool = True) – True if a cancel button should be offered

Return type:

bool

Returns:

True if successful