Date Sun 22 November 2009

Currently when you capture vector data in QGIS 1.3, you have some options to control the way attribute data is input. For example you can have a key value list and then the user sees a combo box with a list of possible values for that field. When an item is selected, the key part is saved into the attribute table. There are various other options which make the attribute capture process quite flexible. Fast forward a little now to the recent hackfest we had in Vienna. Juergen Fischer was working on a much more flexible approach for controlling the user interface experience when new attribute data is required. Juergen's code is in SVN now (the source code repository for QGIS) and this evening I took his new tools for a little test drive.

Getting started

In order to be able to try this out, you need to build the QGIS SVN trunk codebase, or get the nightly build from OSGEO4W. Lets start by creating a simple point layer with two attributes: Name (text) and Age (text).

Screenshot-New Vector Layer

Capture a couple of points to your new layer using the digitising tools so that we have a little data to play with. You should be presented with the default QGIS generated attribute capture form each time you capture a new point.

Default QGIS Attributes Dialog

Creating your customised form

Ok so now we want to create our own custom form for the attribute data capture phase. To do this you need to have Qt4 Designer installed (only needed for the person who creates the forms). Under ubuntu I installed it like this:

sudo apt-get install qt4-designer

Now you can start designer (under Gnome do : Applications -> Programming -> Qt4 Designer). In the dialog that appears create a new dialog. Now drop a lineedit onto the form and set the line edit's widget name in the property list for the widget to 'Name'. Next drop a spinbox onto the form and call it 'Age'. Now any other elements you might want on the form. Finish your form by clicking anywhere on the form and applying a grid layout to it. Lastly save your form remembering its name and where you saved it. I saved my as /tmp/person.ui for the purposes of this blog post but you will likely want to choose somewhere a little more permanent. Here is what my form looked like in designer when I was done:

QGIS Custom Form in Designer

Associating the form to your layer

Ready to have some fun? Jump back to QGIS and double click the layer in the legend to access its properties. Click on 'General' in the property list on the left and then specify the path to your ui file.

Setting the ui in Layer Properties

Now save the layer properties and go and start editing and capture a new point. When you do so you should be presented with your custom dialog instead of the generic one that QGIS usually creates. Also if you click on one of your points using the identify tool, you can now bring up the form by right clicking in the identify results window and choosing 'View Feature Form' from the context menu. If your layer has editing enabled that context menu will show 'Edit Feature Form' rather and you can then adjust the attributes post initial capture.

This is really cool functionality and will be extremely useful in situations where you want to present the form in a very specific way. As a special bonus, if you name a form button after an attribute action, it will cause that action to be launched when the form is presented to the user in QGIS.

From the one more thing dept....

Thats not quite the whole story - with a little python scripting, you can completely customise all the form logic - I will be taking a closer look at that in a future blog post.


comments powered by Disqus