I am using qgis 3.18 and I run the following code in python console.
layer = QgsProject.instance().mapLayersByName("my_layer")[0]
iface.setActiveLayer(layer)
# Define a function called when a feature is added to the layer
def feature_added(fId):
# Disconnect from the signal
layer.featureAdded.disconnect()
layer.select(fId)
features = layer.selectedFeatures()
for f in features:
f['type'] = 'D'
f['surface'] = 'FW'
f['material'] = 'Modular Surface'
layer.updateFeature(f)
layer.removeSelection()
# Save changes and end edit mode
layer.commitChanges()
# disaable supress
formConfig.setSuppress(0)
layer.setEditFormConfig(formConfig)
# Connect the layer to the signal featureAdded, so when a feature is
# added to the layer, the feature_added function is called
layer.featureAdded.connect(feature_added)
# Set the layer in edit mode
layer.startEditing()
formConfig = layer.editFormConfig()
formConfig.setSuppress(1)
layer.setEditFormConfig(formConfig)
# Activate the QGIS add feature tool
iface.actionAddFeature().trigger()
When I run this tool, it works fine and captures the feature in my_layer. Now I do the following operation soon after running the above tool:
- I manually make the layer editable.
- Add a line feature
- Stop editing
- Discard changes
QGIS Crashes! What is wrong in the code??