2

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.

1 Answer 1

2

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

3
  • 1
    Can you please provide an exemplary expression which colours the background colour to red?
    – Erik
    Feb 24, 2022 at 11:20
  • What a really surprising workaround that I would never have thought of myself! For this time it solves my issue, but allow me to critizise that this approach might be very time consuming with the very extensive and nested forms that I have, in case of adding values/colors. If I find a pythonic solution I'll let you know! Feb 25, 2022 at 9:34
  • Indeed, for only a few and simple rules, it's enough but more more complex situations, a python solution or else would be better.
    – Mr_Yum
    Feb 25, 2022 at 10:15

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.