I have the following script that sets the a feature id automatically after a new feature is digitized.
I want the following function to be executed automatically when project is opened (and layers are loaded) so that users don't need to do anything.
import os # Set layer by Name layer=None for lyr in QgsMapLayerRegistry.instance().mapLayers().values(): if lyr.name() == "Pozos": layer = lyr break # Define function to select added feature and add attribute to field "id" def update(featureAdded): idx = layer.fieldNameIndex('id') max = layer.maximumValue(idx) +1 layer.changeAttributeValue(featureAdded, idx, max) # Connect "featureAdded" event to "select" function layer.featureAdded.connect(update)
I tried adding the function inside the project's Macro section inside openProject() function:
But when the project is opened I get:
It looks like QgsMapLayerRegistry is not loaded when the function is triggered?