5.3. Lesson: Forms
When you add new data via digitizing, you're presented with a dialog that lets you fill in the attributes for that feature. However, this dialog is not, by default, very nice to look at. This can cause a usability problem, especially if you have large datasets to create, or if you want other people to help you digitize and they find the default forms to be confusing.
Fortunately, QGIS lets you create your own custom dialogs for a layer. This lesson shows you how.
The goal for this lesson: To create a form for a layer.
5.3.1. Follow Along: Using QGIS' Form Design Functionality
roadslayer in the Layers panel
Enter Edit Mode as before
roadslayer's attribute table
Right-click on any cell in the table. A short menu will appear, that includes the Open form entry.
Click on it to see the form that QGIS generates for this layer
Obviously it would be nice to be able to do this while looking at the map, rather than needing to search for a specific street in the Attribute Table all the time.
Select the roads layer in the Layers panel
The Identify Results panel opens and shows a tree view of the fields values and other general information about the clicked feature.
Now, click again on any street in the map. Along the previous Identify Results dialog, you'll see the now-familiar form:
Each time you click on a single feature with the Identify tool, its form pops up as long as the Auto open form is checked.
5.3.2. Try Yourself Using the Form to Edit Values
If you are in edit mode, you can use this form to edit a feature's attributes.
Activate edit mode (if it isn't already activated).
Edit its highway value to be
Exit edit mode and save your edits
Open the Attribute Table and note that the value has been updated in the attributes table and therefore in the source data
5.3.3. Follow Along: Setting Form Field Types
It's nice to edit things using a form, but you still have to enter everything by hand. Fortunately, forms have different kinds of so-called widgets that allow you to edit data in various different ways.
Open the roads layer's Properties...
Switch to the Fields tab. You'll see this:
Switch to the Attributes Form tab. You'll see this:
Click on the
onewayrow and choose Checkbox as Widget Type in the list of options:
Enter edit mode (if the
roadslayer is not already in edit mode)
Click on the same main road you chose earlier
You will now see that the oneway attribute has a
checkbox next to it denoting
True (checked) or
5.3.4. Try Yourself
Set a more appropriate form widget for the highway field.
For the TYPE, there is obviously a limited amount of types that a road can be, and if you check the attribute table for this layer, you'll see that they are predefined.
Set the widget to Value Map and click Load Data from Layer.
Select roads in the Label dropdown and highway for both the Value and Description options:
Click OK three times.
If you use the Identify tool on a street now while edit mode is active, the dialog you get should look like this:
5.3.5. Try Yourself Creating Test Data
You can also design your own custom form completely from scratch.
Create a simple point layer named
test-datawith two attributes:
Capture a few points on your new layer using the digitizing tools so that you 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.
You may need to disable Snapping if still enabled from earlier tasks.
5.3.6. Follow Along: Creating a New Form
Now we want to create our own custom form for the attribute data capture phase. To do this, you need to have QT Designer installed (only needed for the person who creates the forms).
Start QT Designer.
In the dialog that appears, create a new dialog:
Look for the Widget Box along the left of your screen (default). It contains an item called Line Edit.
Click and drag this item into your form. This creates a new Line Edit in the form.
With the new line edit element selected, you'll see its properties along the side of your screen (on the right by default):
Set its name to
Using the same approach, create a new Spin Box and set its name to
Add a Label with the text
Add a New Personin a bold font (look in the object properties to find out how to set this). Alternatively, you may want to set the title of the dialog itself (rather than adding a label).
Add a Label for your Line Edit and your Spin Box.
Arrange the elements to your own desire.
Click anywhere in your dialog.
Find the Lay Out in a Form Layout button (in a toolbar along the top edge of the screen, by default). This lays out your dialog automatically.
Set the dialog's maximum size (in its properties) to
Your form should now look similar to this:
Save your new form as
When it's done saving, you can close Qt Designer
5.3.7. Follow Along: Associating the Form with Your Layer
Go back to QGIS
Double click the test-data layer in the legend to access its properties.
Click on the Attributes Form tab in the Layer Properties dialog
In the Attribute editor layout dropdown, select Provide ui-file.
Click the ellipsis button and choose the
add_people.uifile you just created:
Click OK on the Layer Properties dialog
Enter edit mode and capture a new point
When you do so, you will be presented with your custom dialog (instead of the generic one that QGIS usually creates).
If you are in edit mode for this layer, that context menu will showinstead, and you can then adjust the attributes in the new form even after initial capture.
5.3.8. In Conclusion
Using forms, you can make life easier for yourself when editing or creating data. By editing widget types or creating an entirely new form from scratch, you can control the experience of someone who digitizes new data for that layer, thereby minimizing misunderstandings and unnecessary errors.
5.3.9. Further Reading
If you completed the advanced section above and have knowledge of Python, you may want to check out this blog entry about creating custom feature forms with Python logic, which allows advanced functions including data validation, autocompletion, etc.
5.3.10. What's Next?
Opening a form on identifying a feature is one of the standard actions that QGIS can perform. However, you can also direct it to perform custom actions that you define. This is the subject of the next lesson.