I want to store values from a virtual field to a static field (the file will be used externally and I need those calculated values from the virtual field hard coded). My idea was to write a python code snippet and add this to the Project macros saveProject(), so when I close QGIS, after working on the layer, the virtual field would be copied to a static field for outside use. I'm new to Python an have come so far:
# Import required modules from qgis.core import edit layer = QgsProject.instance().mapLayersByName('my_trails_z_epsg3857')[0] # index of target field idx = layer.fields().indexFromName('description') with edit(layer): for feat in layer.getFeatures(): # write new_values from virtual field <CP_description> to target field new_value = feat['CP_description'] layer.changeAttributeValue(feat.id(), idx, new_value) However, after running this code in the PY Console for testing, no new values are written to the target field..
descriptionfield the right data type? If you accidentally created an integer field, inserting strings will result inNULLvalues. Is the layer already in edit mode? If so, an assertion error will be thrown bywith editand the code within the block will not run.