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

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Nov 14, 2019 at 21:39
1
  • Thx xunilk, this seems to work perfectly! Commented Nov 17, 2019 at 15:58

1 Answer 1

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.

answered Nov 14, 2019 at 23:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.