I have this code provided by @Joseph but lose it whenever I leave the QGIS and I have to load it on the Python Console whenever reopen QGIS.
What to do to keep this code in a permanent way?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
You can use a project macro to execute python code whenever you open the project.
Navigate to File > Project properties > Macros and insert the code in the
def openProject(): layer = QgsMapLayerRegistry.instance().mapLayer('the_layer_id') def update(): field = layer.fieldNameIndex('mod') e = QgsExpression( " $now " ) e.prepare( layer.pendingFields() ) for feat in layer.selectedFeatures(): feat[field] = e.evaluate( feat ) layer.updateFeature( feat ) layer.attributeValueChanged.connect(update)
You will have to replace 'the_layer_id' with the layer id in project.
You also need to make sure that Macros are executed
Always in the application settings.