[GIS] How to customize QGIS’ Add Feature Widget

editingpyqgisqgis-custom-widgets

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.

Best Answer

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>).

Related Question