2

I am writing a Python plugin for QGIS and I need to be able to retrieve data about the current edits to a particular layer with in QGIS before the edits have been applied.

There does not seem to be any methods in the QgsVector layer or QgsMapLayer classes that allow me to retrieve such data. I am wondering if maybe such a method exists somewhere else.

New contributor
Jonathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Answers 2

4

For that you can use the QgsVectorLayerEditBuffer class which you can retrieve by calling editBuffer on your QgsVectorlayer.

In the following example I print all changed attribute values in my active layer, which is in edit mode:

from qgis.utils import iface
changed_values = iface.activeLayer().editBuffer().changedAttributeValues()
print(changed_values)

Similarly, you can print the changed geometries. Just take a look at the Documentation of QgsVectorLayerEditBuffer.

3

As @juto96’s answer indicates, you can use the QgsVectorLayerEditBuffer to query information about the attributes and geometries changes. In addition, until the changes are committed or rolled back, reading attributes and geometries for features from the QgsVectorLayer will return the modified values. If you need to get the original values while a transaction is open, you can read features from the QgsVectorLayer.dataProvider() (which not surprisingly returns a QgsVectorLayerDataProvider instance).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.