I am relatively new to QGIS. I use conditional formatting for various problems. In the qgis query editor, I would like to define a string variable for further usage, so I wouldn't have to retype the column name countles times.

Example: A = 'B' -- doesn't work

    WHEN A LIKE   '<1,25'  THEN  color_rgb(1,2,3) 
-- A should be a column name

You can define a layer variable as it's explain in Nyalldawson blog. Then use the variable name with @ decorator in Calculator.

For exemple :

  1. Let's define the column_name variable in layer properties > variables panel to a real column name of your layer.

enter image description here

  1. Then in Calculator I set the conditional code with this new variable (like @Chrisl show in his answer).

enter image description here

For your next layers you have to create and define again the variable in layer properties but only once in this place. enter image description here

As you can see in your calculator when you use the exact same code as the precedent one, the variable change the value :

enter image description here

Why use layer variable than Global or Project variable ?

Because in you case you can define once the column_name variable and keep it as long as you want even if you define in another layer the same column_name variable. If you use a project or global variable, the variable is define for all your layers but you need to redefine the value each time you need to perform your code (not recommended in your case)


You can reference the column by using the @ operator. So in your example:

CASE WHEN @A LIKE '<1,25' THEN color_rgb(1,2,3) END
  • Thanks, that is nice, it also works with double quotes. But I want to define and store the column name in the script. So I wouldn't have to retype @A all over. – Krisselack Aug 16 '17 at 13:05

The problem was double quotes. A solution could be:

CASE WHEN "A" LIKE '<1,25' THEN color_rgb(1,2,3) END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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