Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 4 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

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
add comment

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.