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. basic Follow Along: Using QGIS‘ Form Design Functionality

  1. Select the roads layer in the Layers panel

  2. Enter Edit Mode as before

  3. Open the roads layer’s attribute table

  4. Right-click on any cell in the table. A short menu will appear, that includes the Open form entry.

  5. 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.

  1. Select the roads layer in the Layers panel

  2. Using the identify Identify Features tool, click on any street in the map.

  3. The Identify Results panel opens and shows a tree view of the fields values and other general information about the clicked feature.

  4. At the top of the panel, check the Auto open form for single feature results checkbox in the options Identify Settings menu.

  5. Now, click again on any street in the map. Along the previous Identify Results dialog, you’ll see the now-familiar form:

    ../../../_images/roads_form.png
  6. 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. basic 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.

  1. Activate edit mode (if it isn’t already activated).

  2. Using the identify Identify Features tool, click on the main street running through Swellendam:

    ../../../_images/main_street_selected.png
  3. Edit its highway value to be secondary

  4. Exit edit mode and save your edits

  5. 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. moderate 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.

  1. Open the roads layer’s Properties…

  2. Switch to the Fields tab. You’ll see this:

    ../../../_images/fields_panel.png
  3. Switch to the Attributes Form tab. You’ll see this:

    ../../../_images/attributes_form.png
  4. Click on the oneway row and choose Checkbox as Widget Type in the list of options:

    ../../../_images/checkbox_selected.png
  5. Sauskite Gerai

  6. Enter edit mode (if the roads layer is not already in edit mode)

  7. Click on the identify Identify Features tool

  8. 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 False (unchecked).

5.3.4. hard Try Yourself

Set a more appropriate form widget for the highway field.

5.3.5. hard Try Yourself Creating Test Data

You can also design your own custom form completely from scratch.

  1. Create a simple point layer named test-data with two attributes:

    • name (text)

    • age (integer)

    ../../../_images/new_point_layer.png
  2. 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.

    Pastaba

    You may need to disable Snapping if still enabled from earlier tasks.

    ../../../_images/new_point_entry.png

5.3.6. hard 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).

  1. Start QT Designer.

  2. In the dialog that appears, create a new dialog:

    ../../../_images/qt_new_dialog.png
  3. Look for the Widget Box along the left of your screen (default). It contains an item called Line Edit.

  4. Click and drag this item into your form. This creates a new Line Edit in the form.

  5. With the new line edit element selected, you’ll see its properties along the side of your screen (on the right by default):

    ../../../_images/qt_line_edit.png
  6. Set its name to name.

  7. Using the same approach, create a new Spin Box and set its name to age.

  8. Add a Label with the text Add a New Person in 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).

  9. Add a Label for your Line Edit and your Spin Box.

  10. Arrange the elements to your own desire.

  11. Click anywhere in your dialog.

  12. 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.

  13. Set the dialog’s maximum size (in its properties) to 200 (width) by 150 (height).

  14. Your form should now look similar to this:

    ../../../_images/qt_example_form.png
  15. Save your new form as exercise_data/forms/add_people.ui

  16. When it’s done saving, you can close Qt Designer

5.3.7. hard Follow Along: Associating the Form with Your Layer

  1. Go back to QGIS

  2. Double click the test-data layer in the legend to access its properties.

  3. Click on the Attributes Form tab in the Layer Properties dialog

  4. In the Attribute editor layout dropdown, select Provide ui-file.

  5. Click the ellipsis button and choose the add_people.ui file you just created:

    ../../../_images/provide_ui_file.png
  6. Click OK on the Layer Properties dialog

  7. Enter edit mode and capture a new point

  8. When you do so, you will be presented with your custom dialog (instead of the generic one that QGIS usually creates).

  9. If you click on one of your points using the identify Identify Features 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.

  10. If you are in edit mode for this layer, that context menu will show Edit Feature Form instead, 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.