7

This is a follow-up based on the question asked and answered here: (Using attributes tables as variables on QGIS),

I named all my layouts according to the "map_code" column + a short for the theme:

  • A1_HIDRO
  • A2_POPUL
  • A3_TOPOG

enter image description here

Since the trigger for the exhibition is the 'maptype' variable, I thought about extracting that value from somewhere else. So, I tried inputting the substr() function to extract the needed piece of the string from the layout name, as follows:

[%substr(@layout_name,2,1)%]

enter image description here

It happens that I got the expected results on regular textboxes (1, 2, 3, etc), but the variables can't seem to compute that expression and set the appropriate output based on the result of that operation. I wonder if they just can't do that. Am I missing something?

Or there's a workaround to link the value being used on the 'maptype' variable with the name of the current layout?

enter image description here

This way, I'd just need to name a new layout and make it match the value of 'maptype' column.

1 Answer 1

7

Interesting question, as far as I know these variables can only be static string values. But you can pass these string values to eval() to evaluate as an expression.

First store your @maptype variable as just the expression, without the [% on either side.

Now when you refer to the variable in a text box or data defined override as @maptype you will get a string output with the expression text - i.e., substr(@layout_name,2,1) - but it won't treat it like an expression. It's like the equivalent of seeing cells in Excel that say =A2*100 instead of showing the result.

To get around this use eval(@maptype) instead, any time you would otherwise use a direct reference to the variable. Now you should get the actual output of that expression and it will change dynamically based on the input fields/variables.

Note you can't get around this by saving the variable as eval(substr(... - the output is still a string result, not the result of an expression.


Example table set up per linked question in OP: enter image description here

Example output using eval(@maptype) in text boxes and map item themes: enter image description here

1
  • That worked exactly like I wanted, thank you very much! (:
    – silf
    Nov 28, 2023 at 13:59

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.