I have different people using predifined layout. Some of them are on QGIS 3.16 and some on 3.22.

In the layout, I want to use something like that:

WHEN  @qgis_short_version <3.22 THEN 'Fond de carte: © Swisstopo'
WHEN map_credits('carte1') IS NULL Then ''
ELSE 'source: © '||array_to_string( array_sort( map_credits('carte1')), ', ')

The problem is that on 3.16, it can't evaluate the expression cause map_credits()doesn't exist.

Is it a way to do that? Meaning if I'm on 3.16, it evaluate only the first "WHEN", and not the next one?

  • 1
    You could use try() expression.
    – MrXsquared
    Aug 18 at 12:42
  • I don't understand how to use this expression... it only return true/false (1/0).
    – katagena
    Aug 18 at 12:55
  • 1
    From help for function try: "Tries an expression and returns its value if error-free. If the expression returns an error, an alternative value will be returned when provided otherwise the function will return NULL." Syntax: try(expression[,alternative]), see: docs.qgis.org/3.22/en/docs/user_manual/expressions/…
    – Babel
    Aug 18 at 13:22
  • With try(), it doesn’t work, cause in 3.16, it says invalid expression, and not « error ».
    – katagena
    Aug 19 at 7:17

1 Answer 1


The basic idea to solve the problem contains of three basic ideas:

  1. Create the map_credits() part as a string: as long as you create it as a function, QGIS 3.16 will not recognize the function and prompt an error.

  2. Then use the function eval() (see documentation) to convert the string to a function.

  3. The eval()-function should be called only for QGIS versions larger than 3.x (I don't know exactly when map_credits() was introduced). Use an additional if-condition for that. So the string containing map_credits() will only be evaluated (converted from string to actual function) for QGIS versions > 3.x.

The expression will look something like this (see the map_credits() part on line 11) - this exact expression works in my QGIS 3.16 installation.

Be aware: inside the eval() function, everythis is a string, enclosed in single quotes '. Single quotes that appear inside this string must be masked by two additional single quotes - thus ''' (3 single quotes) evaluates as ' (1 single quote), '''' (4 single quotes) as '' (2 single quotes = empty string).

if (
    @qgis_short_version < 3.18,
        WHEN  @qgis_short_version <3.22  -- change the QGIS version here
        THEN 'Fond de carte: © Swisstopo'
        WHEN  @qgis_short_version <3.22 
        THEN ''Fond de carte: © Swisstopo'' 
        WHEN map_credits(''carte1'') IS NULL 
        THEN ''''
        ELSE ''source: © ''||array_to_string( array_sort( map_credits(''carte1'')), '', '')

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.