I have the following script that sets the a feature id automatically after a new feature is digitized.
I added the function inside the project's Macro section inside openProject() function:
The function is being triggered ok every time I open the project. My problem now is that every new feature I create gets the same id (9707).
It seems like max varibles' value is not being updated after a new feature is added and without "Toggle editing" or "Saving layer edits".
def openProject(): from qgis.core import QgsMapLayerRegistry import os # Set active layer #layer = qgis.utils.iface.activeLayer() 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 "Name" def update(featureAdded): idx = layer.fieldNameIndex('id') layer.changeAttributeValue(featureAdded, idx, layer.maximumValue(idx) +1) # Connect "featureAdded" event to "select" function layer.featureAdded.connect(update) def saveProject(): pass def closeProject(): pass