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.

asked Nov 7, 2016 at 15:33
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. Commented 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. Commented Nov 8, 2016 at 14:51

2 Answers 2

5

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

answered Nov 8, 2016 at 15:23
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

answered Nov 7, 2016 at 15:44
4
  • 1
    Thanks 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 dialogue Commented Nov 8, 2016 at 8:41
  • 1
    All these options can also be set from pyqgis ;) Commented 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? Commented Nov 8, 2016 at 14:26
  • Would you tell me the code for QGIS 3.26 version in pyton Commented Jun 16, 2023 at 9:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.