4

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.

2
  • 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
    Nov 8 '16 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
    Nov 8 '16 at 14:51
4

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

2

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)

Edit widgets

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.