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.

  • 1
    @Matthias Thanks for this! As a plugin developer I was interested in the feature. However, I note that RoadForm.py needs to be located in the Project's directory. Is it possible to define an init function from a Plugin?
    – NZ Farmer
    Sep 7, 2014 at 0:23

1 Answer 1


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

  • 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, 2013 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, 2014 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 (docs.qgis.org/testing/en/docs/pyqgis_developer_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, 2014 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, 2014 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. Jul 2, 2014 at 8:03

Your Answer

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

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