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>)
.
Best Answer
Yes, it's possible to customize the QGIS UI from a Python plugin by both adding your own toolbars and menus as well as removing/hiding QGIS toolbars and menus.
These would be the code snippets for each situation:
Adding a toolbar:
Removing a QGIS toolbar:
Adding a menu:
Removing a QGIS menu:
You can append such code to the
initGui()
method of your plugin. I assumed you have an action created in such method, as any plugin has.You can see these code snippets implemented in a test plugin that I've created and published here. In the README file you find instructions for both installing and using it.
Note: You can get a reference of QGIS menus and toolbars from Python by using methods exposed by
iface
. A list of such methods can be found in the QGIS docs.