Skip to content

Custom attribute forms

Introduction

This module is designed to give you an introduction to creating custom attribute forms in QGIS. This function is useful for improved digitizing and for use in data collection apps such as QField and Mergin.

At the end of this module, you should have an understanding about how to create custom attribute forms in QGIS.

What you should already know

Familiarity with Working with attributes and Expressions is highly preferred.

Custom attribute forms in QGIS

When viewing, digitizing, or editing vector data in QGIS, you are provided with a dialog that allows you to view, edit, or input the attributes of a feature—this is often called the Attribute Form.

The default form view for adding new attributes in QGIS (data is GADM adm2)

Figure 1. The default form view for adding new attributes in QGIS (data is GADM adm2)

However, this default dialog may not always align or be the best option for your specific needs in terms of usability and aesthetics. This can present challenges especially when dealing with large layers or when involving collaborators who find the default forms less user-friendly.

Thankfully, QGIS allows you to fully customize Attribute Forms: from changing how the form looks to how individual fields are filled up. This allows you to optimize the data entry experience, streamline your workflow, and make it more accessible for others to collaborate with you.

Custom attribute forms

To see or edit a layer’s attribute form, right click on the layer > Properties > Attributes Form

Attributes Form tab of the household_survey layer

Figure 2. Attributes Form tab of the household_survey layer

Changing the interface

QGIS provides three ways to create the interface of the attribute form dialog.

Options for changing the interface of the attribute form

Figure 3. Options for changing the interface of the attribute form

Autogenerate

Uses the default view created by QGIS

Drag and Drop Designer

Allows the user to edit the view based on options provided by QGIS such as:

  • Tabs: arranges fields into different panes in the attribute form
  • Groups: arranges fields vertically but grouped into different sections
  • Rows: arranges fields as one row
  • Control visibility/collapsed by expressions: use an expression to control if a tab/group will be visible or if a group will be shown similar to conditional questions

Provide ui-file

Use a custom User Interface (.ui) file made with Qt Designer

Creating a custom UI w/ Qt Designer

Figure 4. Creating a custom UI w/ Qt Designer

Attribute parameters

When you click on one of the layer’s fields, a panel will appear showing the different parameters that you can change.

Attribute parameters for the date_surveyed field

Figure 5. Attribute parameters for the date_surveyed field

General

Under General, you can specify:

  • Alias: the text that will be used as label instead of the field name
  • Editable: specify whether the field is editable or not
  • Reuse last entered value: use the last entered value when a new feature is created
  • Label on top: put the label on top instead of to the the left

Widget Type

There are several widget types for getting field values depending on the field type.

The different Widget Types available for the date_surveyed field (datetime field type)

Figure 6. The different Widget Types available for the date_surveyed field (datetime field type)

Examples of these widget types include:

  • BLOB (Binary): uploads files/binaries directly to a database/data source
  • Attachments: stores the path to a file/attachment
  • Unique values: creates a dropdown list where a user can select a value but constrained/limited to the existing values of the layer’s field
  • Value map: creates a dropdown list where a user can select a value based on a predefined list which can be (1) loaded from a layer, (2) loaded from a CSV, or (3) created manually
    • example: list of provinces/cities/barangays
  • Value relation: similar to lookup table or database foreign key: the value column is what’s shown in the dropdown/selection and key column is what’s stored in the field

Constraints

Constraints allow you to provide parameters to limit the values that will be accepted as valid by the field.

Defaults

Defaults allow you to specify the default value for the field when a new feature is created.

Policies

Policies allow you to specify what to do when actions are made on the layer.

Scenario: Simple household survey

In the next exercises, you will simulate a simple household survey with the following fields to collect:

  • date_surveyed (date surveyed, datetime)
  • enumerator (enumerator, string)
  • hh_number (household number, string)
  • hh_photo (house photo, BLOB)
  • num_hh_members (number of household members, integer)
  • hh_estimated_income (estimated household income, integer)
  • barangay (barangay, string)
  • ownership_status (ownership status, string)
  • electricity (electricity, boolean)
  • water_source (water source, string)
  • sanitation (sanitation, string)
  • monthly_electricity_bill (monthly electricity bill, integer)
  • monthly_water_bill (monthly water bill, integer)

Your final attribute form should have the fields divided into 4 groups with the following widgets.

  • Survey Information
    • date surveyed
    • enumerator
  • Household Information
    • household number
    • house photo
    • number of household members
    • estimated household income
    • number of household members
    • ownership status
  • Utilities and Services
    • electricity
    • water source
    • sanitation
  • Utilities and Services Information (should only appear if electricity is true or water source is service provider)
    • monthly electricity bill
    • monthly water bill

Exercise 5.1: Create an attribute form with custom aliases and basic widgets

Level: Beginner

In this exercise, you will edit the attribute form by changing the aliases and using some basic widgets.

FieldParameters
fidAlias: Date Survey, Widget Type: Hidden
date_surveyedAlias: Date Survey, Widget Type: Date/Time
enumeratorAlias: Name of Enumerator, Widget Type: Value Relation (enumerators table)
hh_numberAlias: Household Number, Widget Type: Text Edit
hh_photoAlias: Photo of House, Widget Type: BLOB
num_hh_membersAlias: Number of Household Members, Widget Type: Range
hh_estimated_incomeAlias: Estimated Income of Household, Widget Type: Range
barangayAlias: Barangay, Widget Type: Value relation
ownership_statusAlias: Ownership Status, Widget Type: Value Map
electricityAlias: Does the Household have Electricity?, Widget Type: Checkbox
water_sourceAlias: How does the Household get Water?, Widget Type: Value Map
sanitationAlias: What is the Sanitation Service Level of the Household?, Widget Type: Value Map
monthly_electricity_billAlias: What is the Estimated Monthly Electricity Bill of the Household?, Widget Type: Range
monthly_water_expenseAlias: What is the Estimated Monthly Water Expense of the Household?, Widget Type: Range

This is how your new attribute form should look like

Figure 7. This is how your new attribute form should look like)

Exercise 5.2: Create a custom attribute form with tabs, groups, and widgets

Level: Intermediate

In this exercise, you will group your attribute form to create a better-looking interface and apply some conditions.

  1. On the Attributes Form tab, select Drag and Drop Designer.
  2. On the Form Layout panel, click the green plus button (Add new tab button) and create a new Tab container for the Survey Information group.\

Create new tab

Figure 8. Create new tab)

  1. Create 3 more Tab containers for the Household Information, Utilities and Services, and Utilities and Services Information groups.

Tabs added to Form Layout

Figure 9.Tabs added to Form Layout)

  1. Still in the Form Layout panel, drag the fields so that they are under their corresponding groups/tab.

Attributes ordered using Tabs

Figure 10. Attributes ordered using Tabs)

  1. Try to add a new feature. Your attribute form/dialog should look like below.

Attributes ordered using Tabs

Figure 10. Attributes ordered using Tabs)

  1. For this step, you want the Utilities and Services Information group to be hidden by default unless the answer to electricity is True or the water source is Piped or Water refilling station. To do that, you need to apply Control Visibility by Expression on the Utilities and Services Information tab.
  2. To do this, apply the following expression in the Control Visibility by Expression parameter:
electricity OR water_source in ('0001', '0021')

Control visibility of Utilities and Services Information tab

Figure 11. Control visibility of Utilities and Services Information tab)

  1. Try and add a new feature. Notice what’s different.

No Utilities and Services Information if no electricity or water source isn't piped/water refilling station

Figure 12. No Utilities and Services Information if no electricity or water source isn’t piped/water refilling station)

Utilities and Services Information tab appears if electricity is true or water source is piped/water refilling station

Figure 13. Utilities and Services Information tab appears if electricity is true or water source is piped/water refilling station)

Bonus: Auto-populate fields using QGIS expressions

Level: Advanced

There are several ways to auto-populate fields while digitizing or adding new features. One is to Reuse the last entered value which is useful if the value of a field rarely changes between collection points. For example, if you know that the data collection will mostly be done within a few barangays, it might be good to set the barangay field to use the most recent entered value to save time.

Another option is to provide default values. Using default values can be as simple as providing a single value each time a new feature is added. However, default values in attribute forms are not limited to single values. In fact, the power of having default values really shine when utilized in conjunction with QGIS expressions.

Whether you’re digitizing data on your desk or collecting data on the field with QGIS using QField or Mergin, the task of adding new features to a layer is often cumbersome and time-consuming—especially if you need to manually add fields to the features you digitize.

Try to implement auto-populating fields in your attribute forms. You can refer to Expression Exercise 4.2. Auto-populating fields when digitizing based on another layer.

Certification and support

Contact us or sign-up to our courses if you are interested in having this as an instructor-led or self-paced course.