Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know it is possible to associate a layer with a custom UI for editing feature attributes using the QGIS interface. But is this possible with Python?

This link shows how to achieve the result manually. But I would like to be able to create layers automatically and have them use custom UI without having to redefine the ui file and method each time.

share|improve this question
@Matthias Thanks for this! As a plugin developer I was interested in the feature. However, I note that needs to be located in the Project's directory. Is it possible to define an init function from a Plugin? – NZ Farmer Sep 7 '14 at 0:23
up vote 10 down vote accepted

Form layout

You have the following methods to do this:

QgsVectorLayer.setEditForm( '/path/to/your/ui/file' ) to provide a UI file and QgsVectorLayer.setEditFormInit( 'python.Function' ) to provide a python init method

So having a QgsVectorLayer vl the following will do what you are looking for

vl.setEditForm( '/home/me/uifile.ui' )
vl.setEditFormInit( 'RoadForm.formOpen' )

Starting from QGIS 2.0 you will also have:

QgsVectorLayer.setEditorLayout( QgsVectorLayer::EditorLayout )

Provide QgsVectorLayer.GeneratedLayout, QgsVectorLayer.TabLayout or QgsVectorLayer.UiFileLayout to specify what kind of layout you want. This lets you specify the new drag and drop designer (TabLayout).

Widget layout

Update: starting from QGIS 2.4 there will be new methods (QgsVectorLayer.setEditorWidgetV2 and setEditorWidgetV2Config) to change an editor widget type and its config. The method described here will still work but is marked as deprecated.

There are a number of methods related to the specific widgets you want to use:

Use QgsVectorLayer.setEditType( fieldIdx, editType ) to specify the edit type you want to use for the widget for field index fieldIdx. editType is one of these.

To further configure the widget behavior use the following methods (depending on the available widget configuration options of course):

share|improve this answer
Hi Matthias, Thank you for this very complete looking answer. Certainly looks like you've answered my question and more besides. Allow me a bit of time to work through the methods, and I'll award the bounty. – Dan Jul 30 '13 at 12:54
@Matthias Kuhn Is it possible to add a list of values from a csv to valueMap() with QGIS 2.0 or 2.2? Also, would is it possible to programmatically update the list with newly user-added values? – user25976 Jul 1 '14 at 18:59
@Matthias You are one of QGis developers, yes? This information is very useful - maybe this should be added to the QGis cookbook ( Currently I took a lot of google'ing to find this question and the API did not help much (especially about the difference between EditForm, EditorLayout, EditorWidgetV2). – Rauni Jul 2 '14 at 5:50
@Matthias When You said " The method described here will still work but is marked as deprecated.", did You reference to the method setEditorLayout or methods setEditForm + setEditFormInit? – Rauni Jul 2 '14 at 6:01
@Rauni Thank you for the input with the Cookbook. setEditForm, setEditFormInit and setEditorLayout are not deprecated. Only the widget related methods (setEditType, valueMap, widgetSize etc) except for setFieldEditable and setLabelOnTop. – Matthias Kuhn Jul 2 '14 at 8:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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