3

I have a polygon layer that represents tree/shrub planting sites that were done. As part of the attribute table, I have fields representing things like tree density, species, soil work, tree size, etc. Taking tree sapling size (height) as an example field, my value map looks like:

< 1m

1m

,>1m (ignore comma)

Currently, in the Attribute Form, I simply have a Value Map widget type, making it possible to select 1 of the 3 value options.

However sometimes, we plant a mix of tree sizes. Is there a way to make the value map a checklist, so that we'd be able to choose 2, or all 3, of the above value options?

I am using QGIS 3.4.

2
  • Is your question answered here: gis.stackexchange.com/questions/306881/… ?
    – csk
    Commented Jan 6, 2020 at 18:51
  • Unfortunately no, I indeed saw that thread but that's simply about (to my understanding) linking a dropdown to an existing file/column. What I'm looking for is to select multiple items in a dropdown.
    – Robert
    Commented Jan 6, 2020 at 19:16

1 Answer 1

4

For this task you could use a custom form with some Python/PyQt logic. If you are not at all familiar with this process, I would suggest having a look at this blog post from Nathan Woodrow.

It was written a while ago and is for older versions of QGIS and PyQt but the basic workflow is still very much the same.

I have created an example based on your question which should help you to implement what you are after.

Firstly, I have a polygon layer called 'Planting_sites' whose attribute table looks like this:

enter image description here

The first step is to create a form in Qt Designer (which should come with your QGIS installation- on Windows can be found in C:/OSGeo4W64/bin). Drag and drop labels and widgets for each attribute field, and change the object name of each widget to match the field name. For the tree size classes you will need a QListWidget, and a QLineEdit to hold the selected sizes. Make sure to change the object name of the line edit to match the associated field name, in this case 'Tree_size'.

Example:

enter image description here

Save this .ui file in the same directory as your project file. Mine is called 'Planting_site_form.ui'

Now you need to create a python file to handle the logic of populating the list widget with the size class items and filling the 'Tree_size' line edit widget from the selected items of the list widget. For this you can use your favorite text editor, IDE or the QGIS Python console.

My Python script contains the following code:

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

dlg = None
size_field = None
list_widget = None

def formOpen(dialog, layer, feature):
    global dlg
    dlg = dialog
    global size_field
    size_field = dlg.findChild(QLineEdit, 'Tree_size')
    size_field.setReadOnly(True)
    global list_widget
    list_widget = dlg.findChild(QListWidget, 'listWidget')
    list_widget.addItems(['<1m', '1-2m', '>2m'])
    list_widget.setSelectionMode(QListWidget.ExtendedSelection)
    list_widget.itemClicked.connect(selection_changed)

def selection_changed():
    items = [i.text() for i in list_widget.selectedItems()]
    size_field.setText(', '.join(items))

Save this as a python file in the same directory. I called mine 'Planting_sites_logic.py'.

Now open the form tab of the layer properties dialog. Select 'provide ui-file' and, under 'Edit UI', load the .ui file you saved earlier. You also need to specify the Python file and the function name which provides the logic (see images below).

enter image description here

enter image description here

Now when adding new features or editing existing features, the form looks like below. You can use ctrl to select multiple size classes from the list widget and write these to your attribute table!

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.