Difficulty = if I can do it, you can (based on the: ‘can I do it scale’!)
I’ve been experimenting with dialogue box design just lately for two reasons:
- I wanted a more intuitive box for the others in my team to be able to enter data based on a workflow rather than the standard straight down approach and to provide check and drop-down boxes for some of the fields.
- the second reason was for the need to enter data quickly in just two fields, on a table that had loads of fields, the two fields would have been at the top and near the bottom respectively on a normal dialogue and so was a pain tabbing all the way down.
A little research (mainly reading Nathan Woodrow’s blog posts on the subject – don’t be put off by the Python stuff, use the link in his post to the original – you’ll get the idea). This shows how to link (called binding) the bits (text boxes etc.) on a form to the fields in a table (this bit is the most important ’cause it don’t work if you don’t get it right!).
What you need.
To design a form you will need QT. If you download and update your installation (MS Windows – I don’t know how this works on a MAC or Linux) using OSGeo4W network installer, it is an option in the ‘advanced’ section – if it isn’t installed, go through the setup process and select the options to install QT.
I don’t propose to go through QT in depth but show the main bits – on starting you will see the start page. You will invariably select the default or open an existing form – if you already design forms then don’t go any further unless you want to feedback (welcomed) on my instructions.
Designing your first form
Selecting the default gives you this:
On the left you will see all the ‘widgets’ that can be added to a form (drag them onto a form) – here I have added ‘Label’ and ‘Line Edit’ widgets. The panels on the right are all the properties associated with the selected widget (or form, if you haven’t got a widget selected) so the content changes depending on what you have selected.
You will add as many widgets as you want and arrange them how you want, you can make the dialogue box bigger or smaller etc. etc. – have a play. Once you have done, or if you want to come back to it, save the file – it creates a ‘.UI’ file which can be opened next time you start QT.
The important bit – linking to a layer!
To use the form with your layer (I’m using a single table that I created for this example) in QGIS you have to link the ‘Line Edit’ widget to a field in your table.
If you look in the ‘Object Inspector’ panel, on the right, select the widget, it will be highlighted in the panel, you will see it is called ‘lineEdit’, double click this and it highlights blue (on mine), you change the ‘lineEdit’ text to the name of the field in your table – it must be spelt correctly and be the same case as your field name.
To change the label text, you need to select the label on the form and look in the ‘Property Editor’ on the right. Scroll down to the ‘QLabel’ part and look for the ‘text’ property (the first on that part of the panel). Click into the property and change the value – it changes on the form.
That’s it! You’ve created a custom form. Save your edits.
Testing it in QGIS
Go into QGIS and open your layer. Open the layer properties and go to ‘Fields’.
You should be familiar with this if you use QGIS. For a ‘lineEdit’ widget you don’t need to change anything – in the next episode I will go over using drop-downs and check boxes, for now though you can leave things as they are.
So the field I am linking to is the ‘maptext’ field so I call my ‘lineEdit’ widget ‘maptext’.
I’ve changed the label so that users will understand what the input is…you get the picture.
Linking the form to the layer.
We now need to tell QGIS to use the ‘.UI’ form when editing or displaying the information in a form.
Use the ‘Attribute editor layout’ drop down and specify ‘Provide UI-file‘.
Another box will appear asking for the path to the ‘UI’ file.
Select the ‘…’ button and navigate to where you saved the ‘UI’ file and select it -you’ve now told QGIS to use this form for this layer.
How does it work?
In a map, add a feature to the layer or, if it is an existing layer, use the ‘identify’ button and click on an existing feature, up will come the dialogue you created – here I’m adding a feature.
Note: the dialogue will open at the size of the last dialogue you opened in QGIS so if it is not big enough, expand it by selecting the bottom right corner – I find this a particularly annoying trait as I would expect the dialogue to open at the size I designed!
So I’ve added a feature, the dialogue appeared, I enter the text as shown, clicked OK.
For this example, I’ve labelled the features with the field into which this text is inserted and also show the table – the text I typed into the dialogue is in the field in the correct record – this way, even if you have 100 fields in your table, you can design a quick data capture form and include only those fields for which you want to enter data.
Dialogue doesn’t appear:
- check you have told QGIS to use a custom dialogue
- check you have specified the correct path to the correct ‘UI’ file
Data not going into the table:
- check that the ‘lineEdit’ widget is called and spelt exactly the same as the field name into which you want the information – this is the most likely cause of data not going into the table.
- check you have the correct layer selected in the ‘Layers’ panel
- I don’t know – double-check everything!
BTW: you can keep QT open with the form showing whilst you test it in QGIS, just make changes to the form or add new stuff, save the form, overwriting the previous, and it will appear with the changes as soon as you inspect or add a feature in QGIS.
I hope you found this useful. Next time (soon) I will go into using other widgets but for now, getting these basics sorted will help you get used to the process…have fun!