There are many questions like this but either they propose to implement a new method to update values or describe how to set default values for fields in GUI.
I'm writing a QgsProcessingAlgorithm and need to set default values when modifying fields using the QgsEditorWidgetSetup either for 'KeyValue' if the fieldtype is hstore or 'TextEdit' if it's some kind of String-field. Here's what I've tried (for 'TextEdit' because it's simpler):
sourceLayer.setEditorWidgetSetup(index, QgsEditorWidgetSetup('TextEdit',
{
'DefaultValue': 'DefValue',
'Defaults': 'DefValue',
'Default': 'DefValue',
'IsMultiline': 'True',
'UseHtml': 'False'
}
The properties 'IsMultiline' and 'UseHtml' work flawlessly but none of the defaults is accepted.
A often proposed approach is to use the Python console to get the QgsEditorWidgetSetup config of a field configured via GUI. This is done by the following code:
layer = qgis.utils.iface.activeLayer()
layer.fields().names()
idx = layer.fields().indexFromName('text')
field = layer.fields()[idx]
field.editorWidgetSetup().config()
Unfortunately the output is just:
{'IsMultiline': False, 'UseHtml': False}
Although of course the default value I configured is working fine when I add new features through the attribute table.
Any ideas?