QGIS 3.22 – Troubleshooting Dropdown List Issues in Attribute Forms

dropdownlistqgisvalues

I discovered the nice possibility in QGIS to create dropdown lists in a field, with which I (should) choose in a simple way one of several species (in my case) – while entering new features. I tried these two possibilities:

  1. I define in attribute form the widget "unique values" – you see this screenshot
    enter image description here

it works (nearly) fine, while entering a new features I start typing the name of a species – and I get offered all species that I already have in this Shapefile.
The offer looks like that
enter image description here
It is a great help like that – but: the offered species are not sorted alphabetically – and I see only 7 species at a time. Is there a possibility to change that?

  1. I tried then to choose in attribute form the widget "value relation" – I linked a csv-table with all species I need to this field. Screenshot here:
    enter image description here
    The advantage with this possibility is, while entering a new feature I get – sorted alphabetically – a great number of species, and I can choose one of them.
    But when I look at the attribute table it looks like that
    enter image description here

most of the species that has been in the shape file before – are in brackets. And the new entered species of the csv-table (and even some of the "old" species – here Meum athamanticum) are not in brackets. That is no good way to work with that further.

Has anybody an idea how to use (especially the 1. variant) more comfortable? (alphabetical, and more species in the dropdown list?

Best Answer

To get a dropdown list of values used in your layer but in an alphabetical order, one workaround is to use the Value Relation widget, but then selecting the same layer itself as the Layer under Value Relation, and the species field as the Key and Value columns.

Then to prevent duplicate values appearing in the list, adapt the following expression in the Filter Expression textbox (per this answer):

$id=array_first(array_agg($id,"species_field"))

Example:

enter image description here

enter image description here


However, in both instances, the list will only ever show you what is already in your dataset. If you only have 7 unique species that is all it will show you.

To add species that aren't already in your dataset, you can only do it using the Unique Values widget and would need to enable the 'Editable' option to add additional entries, but then you no longer have a drop down box, just an autocompleter.

For your use case I suspect you would be better off using Value Relation with a master csv of species names. However, it appears you need to use a better master list as the brackets () in your existing dataset indicate values that are in your dataset, but not your master list. I would recommend checking your master list to make sure everything is in it already.

For what it's worth there have been requests to make the Unique Values list sortable since 2018 at least, I don't know that it will be implemented just yet unless you poke one of the developers directly.


Edit: The above solution can be unusably slow on large datasets. In such instances if you still only want to use values in your source layer, try creating a Virtual Layer (Layer > Create > New Virtual Layer)

And use the following query adapted to your dataset:

SELECT species_field FROM source_layer_name GROUP BY species_field

enter image description here

Then use that resulting virtual layer in your Value Relation widget.

enter image description here

It still takes a couple of seconds for the attribute form to open for a source layer shapefile with 45000 features and about 440 unique species values.

enter image description here

The only way to improve the speed would I think be to move the dataset onto an actual server with SQL views that would load a lot faster.