A new QGIS plugin allows dynamic filtering of values in forms

Still hot from our forge: a new QGIS plugin allows dynamic filtering of items in combo boxes in attribute forms.

   
This plugin has been partially funded (50%) by ARPA Piemonte.

Description

This is a core-enhancement QGIS plugin that makes the implementation of complex dynamic filters in QGIS attribute forms an easy task. For example, this widget can be used to implement drill-down forms, where the values available in one field depend on the values of other fields.

Download

The plugin is available on the official QGIS Python Plugin Repository and the source code is on GitHub QGIS Form Value Relation plugin repository

Implementation

The new “Form Value Relation” widget is essentially a clone of the core “Value Relation” widget with some important differences: When the widget is created:
  • the whole unfiltered features of the related layer are loaded and cached
  • the form values of all the attributes are added to the context (see below)
  • the filtering against the expression happens every time the widget is refreshed
  • a signal is bound to the form changes and if the changed field is present in the filter expression, the features are filtered against the expression and the widget is refreshed

Using form values in the expression

A new expression function is available (in the “Custom” section):
CurrentFormValue('FIELD_NAME')
This function returns the current value of a field in the editor form.

Note

  1. This function can only be used inside forms and it’s particularly useful when used together with the custom widget `Form Value Relation`
  2. If the field does not exists the function returns an empty string.

Visual guide

  Download the example project.   This is the new widget in action: changing the field FK_PROV, the ISTAT values are filtered according to the filter expression.
The new widget in action

The new widget drill-down in action

layer_config_fields

Choosing the new widget

Configuring the widget

Configuring the widget

Configuring the expression

Configuring the expression to read FK_PROV value from the form

10 Responses to “A new QGIS plugin allows dynamic filtering of values in forms”

  • Anonymous

    Thanks for this plugin,

    It’s exactly what I need but i do not manage to use it. Would it be possible to describe the layer that you are working with and the field of this layers ? With that I would be able to adapt it to my needs.

  • Eduardo

    Looks like a nice addition! I will test in more detail later. I though on mentioning that when I did a quick test, the ui in the Edit Widget Properties did not load properly while having a locale language for QGIS (Finnish).

  • Casper B. Jensen

    If I try to change the content of an attribute after I’ve created the feature my program crashes as I double-click on the attribute field with a pop-up windows saying
    Minidump written to C: … \Temp\qgis-20160310-071702-9988-4565-99ac688.dmp

    As well when I delete features they are kept in ‘Show feature Count’ and I get this Python-error:

    2016-03-10T07:12:03 1 Traceback (most recent call last):
    File “C:/Users/CasperBJ/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py”, line 357, in createCache
    layer = QgsMapLayerRegistry.instance().mapLayer( self.config( “Layer” ) )
    RuntimeError: wrapped C/C++ object of type FormAwareValueRelationWidgetWrapper has been deleted

    2016-03-10T07:12:24 1 Traceback (most recent call last):
    File “C:/Users/CasperBJ/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py”, line 357, in createCache
    layer = QgsMapLayerRegistry.instance().mapLayer( self.config( “Layer” ) )
    RuntimeError: wrapped C/C++ object of type FormAwareValueRelationWidgetWrapper has been deleted

    2016-03-10T07:12:37 1 Traceback (most recent call last):
    File “C:/Users/CasperBJ/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py”, line 357, in createCache
    layer = QgsMapLayerRegistry.instance().mapLayer( self.config( “Layer” ) )
    RuntimeError: wrapped C/C++ object of type FormAwareValueRelationWidgetWrapper has been deleted

    2016-03-10T07:12:37 1 Traceback (most recent call last):
    File “C:/Users/CasperBJ/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py”, line 357, in createCache
    layer = QgsMapLayerRegistry.instance().mapLayer( self.config( “Layer” ) )
    RuntimeError: wrapped C/C++ object of type FormAwareValueRelationWidgetWrapper has been deleted

    2016-03-10T07:13:52 1 Traceback (most recent call last):
    File “C:/Users/CasperBJ/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py”, line 357, in createCache
    layer = QgsMapLayerRegistry.instance().mapLayer( self.config( “Layer” ) )
    RuntimeError: wrapped C/C++ object of type FormAwareValueRelationWidgetWrapper has been deleted