Creating custom forms in QGIS – Part 1

One of the things I love about QGIS is its ability to easily create custom forms. Usually without writing a single line of code. The QGIS manual in my opinion doesn’t do it justice, as it only describes this functionality very briefly, so I decided to write a couple of posts explaining the process with examples.

I will be using a shapefile containing a road network. The attributes in the shapefile are as below and there are pretty self-explanatory:

field_properties

Now, note the [Edit Widget] field. Its default value for each attribute is “Line Edit” which basically means it will display a simple text box for the user to enter a value when in Edit Mode. Obviously, this is not ideal as we got values like number of Lanes (LANES attribute) which should be numeric values or dates (DATE_OPEN attribute). QGIS won’t validate the form values till after we press the save button.

So first thing we need to do is change the widget (control) that will display in the Edit form for the DATE_OPEN attribute. Press the [Line Edit] button for this attribute and the attribute edit dialog will open. Select [Calendar] from the left panel:

date_control

You can edit the date format to control how the date will be displayed. When finished click the OK button.

Next, I wanted the ROAD_CLASS attribute to be selected from a drop-down list. To do this, follow the previous steps for the ROAD_CLASS attribute and this time select [Unique Values] as the widget. The Unique values widget will display all unique values that already exist in the shapefile in a drop-down list. And as we also need to be able to enter new values, check the [Editable] check box (the second one- the first is checked by default and it controls whether the field would be editable or not in edit mode).

unique_values

Similary the Road environment (ROAD_ENVIR attribute) should be displayed as a drop-down list but this time we will use our own values. To do this select the [Value Map] widget and enter your values in the displayed grid as shown below:

value_map

Then, for the LANES attribute we would use the [Edit Range] widget. Number of lanes can range theoretically between 1 and 6, so put these values as min, max and define a step of 1:

range

But we can also display images or web pages in our form. Not surprisingly the photo widget is called [Photo] and you can defined the width and height of the image to display…

photo

…whereas to display a web page of specific width and height you select the [Webview] widget:

url

 

You can also display a color ramp in your form. To do this, use the [SYMBOL] field in this example and select the [Color] widget:

color

If you followed this far, congratulations! Now close the properties window and start editing. Click on the identify button and click on any of the features in your shapefile. In the identify window right-click on one of the features and select the [Edit Feature Form] option:

open_feature_form

 

Have a look at the form and note how the different fields display different widgets/controls based on wh

at we set them at, in the previous steps

attributes

As some of you may have noticed by now, this is not all the widgets that are available in QGIS but hopefully we covered quite a few of them and you got the general idea.

But this is not all. With QGIS, you can further customize your forms, adding tabs and grouping your controls. That’ll be next, in Part 2

 

 

 

 

4 thoughts on “Creating custom forms in QGIS – Part 1

  1. We have an MVVM framework with forms built in php that we use for attribute edits. We can make these available as a service. Can I configure QGIS to call these forms. I don’t want to introduce a whole set of new forms that then need to be managed as well as the most used ones.

    1. Barry – never tried anything similar but very much doubt it. QGIS is very specific on the forms it uses. Even the ones I talk about in my posts have to be saved under the same folder as the qgs project file. The only way I can think is to write some code where your QGIS custom form will be completely empty apart from maybe a dynamically created hyperlink which will take you to the PHP form. But completing negating the need for qgis-based form… think is a no-go

      1. Hi
        Thanks for the reply.

        We’re investigating. I am hoping to avoid Qgis forms all together if possible.

        Qgis is a tool not the main application and we will not be building new redundant data entry forms. Parts of the data team like having Qgis for spatial editing from time to time and we are hoping to give them a unified environment. But the forms are also used by end users of our application, which is not using Qgis, but can be fully expressed in api’s.

        I’ll try to update on progress.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s