I have a plugin that validates some of the data a user provides when adding a new feature by the build in Add Feature UI. add feature dialogue
I want to disable some of the input fields or provide some default values to some of the fields, or at least let the user know which fields are required. Is there a way to customize the Add Feature input widget?
EDIT
Maybe I wasn't clear enough in my question. I'm looking for a pyqgis
solution, that is, a way to dynamically customize the input form of the Add Feature dialogue.
-
For coding questions we require that you provide your best code attempt and post it in the question and note what errors you are receiving and where you are stuck.artwork21– artwork212016年11月08日 14:41:04 +00:00Commented Nov 8, 2016 at 14:41
-
If I had an idea which API endpoint to use, I wouldn't have to ask here in the first place.LarsVegas– LarsVegas2016年11月08日 14:51:19 +00:00Commented Nov 8, 2016 at 14:51
2 Answers 2
One by one I found all the pieces of the puzzle. This related question on the edit widget provided a good starting point for my quest, as it shows how to change the widget itself
vLayer.setEditorWidgetV2(field_idx, <widget_type>)
So for exampel, if you want to hide a field with index 0
vLayer.setEditorWidgetV2(0, "hidden")
The colum will no longer be visisble in the Add Featre dialogue (note: it will also disappaere from the attribute table)
Il also gives an example on how to pass config paramters to the widget, in this case of type DateTime
.
vLayer.setEditorWidgetV2Config(fieldIndex,
{'display_format': 'yyyy-MM-dd',
'allow_null': False,
'field_format': 'yyyy-MM-dd',
'calendar_popup': True}
)
The editor widget wrapper documentation was pretty hard to find, I have to say. So hopefully this link will be of some use for others on a similiar quest. For TextEdit
there are two paraters. The python API uses the same kwargs
IsMultiline
and UseHtml
However, if you want to disable the editing capability of the field you'll have to use vlayer.setFieldEditable(field_idx, <bool>)
.
If you right-click your layer and go to:
Properties > Fields
You can edit some properties of each of your fields such as:
- Editable - Unchecking this means the user cannot edit values
- Default value - Adds default values (only available from QGIS 2.18)
-
1Thanks for pointing this out but I'm looking for a
pyqgis
solution, that is, a way to dynamically customize the input form of the Add Feature dialogueLarsVegas– LarsVegas2016年11月08日 08:41:49 +00:00Commented Nov 8, 2016 at 8:41 -
1All these options can also be set from pyqgis ;)Matthias Kuhn– Matthias Kuhn2016年11月08日 11:06:48 +00:00Commented Nov 8, 2016 at 11:06
-
@LarsVegas - No problem, maybe the following posts might help: How to implement layer.setEditType for custom widgets in QGIS?; and How to change the edit widget for fields in a layer with PyQGIS?Joseph– Joseph2016年11月08日 14:26:22 +00:00Commented Nov 8, 2016 at 14:26
-
Would you tell me the code for QGIS 3.26 version in pytonBenStone– BenStone2023年06月16日 09:54:36 +00:00Commented Jun 16, 2023 at 9:54