3

In QGIS Graphical modeller there is the option to create user defined variables that one can use in the model. I dont always need to use the modeller to do stuff. Is there a way to have user defined variables in regular QGIS (very good for styling)?

I have found that in model properties I can use Title, Abstract, Author etc. for this, but that is really not very elegant. I just want regular variables that I can set and then use. Really good for visualization works.

1 Answer 1

3

Yes, you can define your own custom variables in QGIS in different levels like as in global Level, as project or layer variables and even as ad hoc variables in QGIS expressions using the function with_variable().

See functions help for with_variable() expression as well as an expample where three different variable are declared: vertex (=20), azimuth1 and azimuth2 (the last two have an expression assigned as values).

Go to the Settings / properties of the desired Level (e.g. right Click layer > properties > tab variables) and select variables to add your own variable by typing a name and the value of the variable. You can then call the variable by its Name preceded with @ - e.g. @my_variable.

See here for more information about custom variables: http://nyalldawson.net/2015/12/exploring-variables-in-qgis-pt-3-layer-level-variables/


The content (value) of the varible can be set to an attribute or a complex expression e.g. to generate all kind of sophisticated data defined styles. So this example: define a project variable called set_color with this value (string):

case 
when value = 1 then 'red' 
when value = 2 then 'blue' 
when value = 3 then 'green' 
else 'yellow' 
end

enter image description here

Now use the variable @set_color to create a data defined color of a layer, based on its attribute field value using the function eval(): eval(@set_color):

enter image description here

Variables you defined at global, project or layer level will appear in expression string builder dialog under variables:

enter image description here

17
  • Very cool, thank you so much. I always thought it was something else, now it makes complete sense. Two more questions then. 1. is there a way to have some of the variable on my interface as a panel or toolbar for quick access? 2. is there a way to assign an attribute to a variable? I often make complicated styling settings that include attributes of the layer and it is a pain to change all of them individually, if I could change them globally through a variable it would be a life saver. But I am afraid it only works with one value, right? Thanks. Sep 17, 2022 at 13:19
  • 1. I'm not sure what you mean by "have some of the variable on my interface as a panel or toolbar for quick access" - if you have another question, best post it as a separate post. 2. You can't only assign an attribute to a variable, but a whole expression, see updated answer.
    – Babel
    Sep 17, 2022 at 13:38
  • Added another edit at the bottom of my answer, maybe this is what you looked for...?
    – Babel
    Sep 17, 2022 at 13:49
  • Lets forget n. 1 for now. Thank you for your edit. However, I am afraid it doesn't achieve much for me. I could just as well add this expression directly and not through the variable, even though its quite cool. What I am looking for is to quickly change part of this expression using variables. Lets use your example - what if I wanted to not use "value" but instead wanted to use a different attribute ("value2"), how do I set it up, so that I can change it in multiple styling options? Can this be achieved through variables? Example of expression of mine: imgur.com/a/8LobxXZ Sep 17, 2022 at 13:59
  • Simply go where you defined the variable and change from value to value2 and wherever you used this custom variable, settings will use field value2 and not value any more. This works even for many different layers at once as long as all of them have a field value2 (or value).
    – Babel
    Sep 17, 2022 at 14:12

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.