“Rule-based” background color in QGIS attribute form containers

attribute-formdata-defined-overrideqgisrule-based

In QGIS attribute form Drag and Drop Designer we can choose a backgroud color for widget containers (Tabs and Group boxes):

enter image description here

Is it possible to have rule based background colors depending on some attribute value, i.e. some sort of data defined override?

I thought of a Python macro too, which sets the background color when a form loads, but I was not able to figure out how to adress the required container widget.

Best Answer

If you check the box above (Control visibility by expression) and setup your rules carefully, you should be able to obtain what you are looking for.

For example : In a table with a field called value, with value between 1 and 5, and three rules with corresponding background color:

  1. value is equal or less than 2 (green)
  2. value is equal to 3 (yellow)
  3. value is more or equal to 4 (red)

I create as many container (green + sign) as I need rules (three here). In each of the container, I check Control Visibility by Expression and setup an expression corresponding to the specific value I want in that container.

enter image description here

Then, if I open the form, the container with the rule matching the value will be visible :

enter image description here


enter image description here