Custom QGIS feature forms – Value Binding


One thing I didn’t explain very well  in my other post was how to correctly set up value binding between your custom form and QGIS.  I didn’t explain it because at the time I didn’t know how.

The other day I was building a custom form QGIS for a project I am working on. I had named all the fields right, set the ui as the edit form for the layer, but only the line edits were getting bound to the correct values.

 So having a dig around in the code I noticed that QGIS uses the same methods to bind the built-in edit forms as it does for the custom forms, meaning that you must set what kind of control you want to use in Layer Properties -> Fields 

Correctly binding values

First create the form with the controls you need, remember to name them the same as your fields.

Custom form with controls using the same name as the fields

Note that here I have a QComboBox with the FeatureCla name, this will bind the combo box to the FeatureCla field in my dataset in QGIS.

Now set the custom form as the Edit UI for the layer

Set the Edit UI to your form

Tip: You can use relative paths if you store the form along side your project file

 Flick to the Fields tab and set up the Edit Widget type for each field that you have used on the custom feature form.

Set the Edit Widget that matches your control

I have set the FeatueCla field to use Unique values widget, this tells QGIS to collect all the unique values from that column and add them to the QComboBox.  There are a range of different edit widgets you can set

Each will map to a different set of control types (Widgets) e.g. If you want to have a checkbox on your form you must select Checkbox in the Edit Widget list to get it to bind correctly.

Save the properties and head back to you map.  Use the Identify Tool to select a feature.

Values bound to form

And that is it. Pretty cool hey!

Final thoughts

This is one feature I really like in QGIS.  The ability to create custom forms for people to do data entry without the need to build a plugin is very cool.  Couple this the built-in GPS module for QGIS and you have yourself a nice simple field data collection program.

I have some ideas to make this feature even more powerful, but more on that later once I get some time to add it in.

5 thoughts on “Custom QGIS feature forms – Value Binding

  1. Now that is pretty cool, Nathan – I agree.
    What if you don’t use a custom edit dialog – will qGIS create a standard edit dialog where all the fields in the table/layer is mapped to a control and can you here also define what kind of control that should be?

    1. Yeah that’s exactly what happens, pretty much just a straight down list with a field name and control. Picking the edit widget will depend on which control you get.

  2. Hi Nathan, Thanks. I have my form working but when I change the attribute, no data were saved. Anything that I missed?

    1. Additional info. My data is shape file. Was able to edit and save changes using Attribute Table .Was able to view data in my custom form in edit mode and when I update value of text box or dropdown, there is no problem. When I press ok, form close. I open the form for same feature value is still old (not updated)

Leave a comment