3

I'm looking for a method to find out how many features in a layer have changed during edit mode (specifically attribute values). For my example i'm using QgsVectorLayerEditBuffer object, method ChangeAttributeValue().

I use editBuffer() method to get the layer buffer and after making some changes, I use ChangedAttributesValues(). This returns 'None'. Example:

layer = iface.activeLayer()

buffer = layer.editBuffer()

..# make changes

buffer.changedAttributeValues()
..None

see also: Get number of changed features in a layer under edit mode using pyqgis

1
  • Thx xunilk, this seems to work perfectly! – krukkie Nov 17 '19 at 15:58
3

Link refers number of changed features in a layer under edit mode so, if you commit changes, buffer (QgsVectorLayerEditBuffer object) will be lost. Complete procedure is as follows:

1) Load layer and in Python Console:

>>>layer = iface.activeLayer()
>>>buffer = layer.editBuffer()
>>>buffer #for corroborating object was produced
<qgis._core.QgsVectorLayerEditBuffer object at 0x7f81da97c948>

2) Click in "Toggle Editing button", open attributes table and make some changes in it; don't commit them. In my case, I only changed in my layer the first id feature. So, following command produces:

>>>buffer.changedAttributeValues()
{0: {0: 1}}

If a new change is produced, but I commit it, result is as follows:

>>>buffer.changedAttributeValues()
Traceback (most recent call last):
  File "/usr/lib/python3.7/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
RuntimeError: wrapped C/C++ object of type QgsVectorLayerEditBuffer has been deleted

For producing several consecutive changes, I change layer id three times in two records:

buffer = layer.editBuffer()
buffer.changedAttributeValues()
{0: {0: 3}, 1: {0: 2}}

However, only two changes were computed because layer has only two features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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