I'm developing an addon for QGIS that needs to assign a UUID to any feature as soon as it is added to the layer. I'm using the featureAdded signal to write the UUID in the corresponding field of the feature.
This is the code that I am using (as simplified as possible):
def run(self): self.iface.mapCanvas().currentLayer().featureAdded.connect(self.onFeatureAdded) def onFeatureAdded(self, fid): layer = self.iface.mapCanvas().currentLayer() layer.beginEditCommand("Set UUID") print layer.changeAttributeValue(fid, layer.fieldNameIndex('guid_pol'), 'some_random_uuid') # prints True layer.endEditCommand()
(I created a new plugin with the 'Plugin Builder' add-on and that above is the only code I have added to it.)
Unless I'm missing something, this follows the guidelines from the documentation in the QGIS developer cookbook: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/vector.html#modifying-vector-layers-with-an-editing-buffer
When I edit the layer, if I add a feature the field 'guid_pol' is set to 'some_random_uuid' as expected. I can commit the changes without a problem, however, if instead of commiting the changes I try cancelling them QGIS crashes with error 0xC0000005 (Access Violation).
Interestingly, the command 'Set UUID' doesn't get added to the undo/redo stack (at least, not to the Undo/redo Panel) and if I try to undo it crashes as well.
I'm using QGIS 2.14 but I observed similar behaviour with QGIS 2.12.
I'm having a hard time getting this to work. Is there something I am doing wrong?