13

I'd like to change the value of a user-defined project variable (can be manually edited in Project Settings | Variables) using the Python console. I tracked down the setVariable() function in the QgsExpressionContextScope class but haven't succeeded in actually changing the variable in the project settings. My code so far:

iface.mapCanvas().mapSettings().expressionContext().scope(0).setVariable('myvar',1)

I guess I'm getting lost in the different expression contexts ...

2 Answers 2

12

Look at QgsExpressionContextUtils (https://qgis.org/api/classQgsExpressionContextUtils.html). The method you need is QgsExpressionContextUtils.setProjectVariable, e.g.

QgsExpressionContextUtils.setProjectVariable('myvar','hello world')
5
  • What about deleting a variable? I didn't find method for it in QgsExpressionContextUtils Commented Jul 24, 2017 at 15:39
  • There's no high level API to do this. Possibly you could get away with NULLING the variable (setting it to None), but if not, you need to use QgsExpressionContextUtils.setProjectVariables({} ) and reset the whole lot. You'd first need to check QgsExpressionContextUtils.projectScope() and create a dict of all the variables you want to keep. It's far from ideal - but would also be a trivial addition to the API if you wanted to get involved in QGIS development and send through a pull request on github... Commented Jul 24, 2017 at 22:58
  • Thanks @ndawson, it works with your work around to reset with setProjectVariables(preserved_variables) I have checked the C++ code, I hope I can contribute for the functionality. Commented Jul 25, 2017 at 2:06
  • 2
    QgsExpressionContextUtils.removeProjectVariable exists now! Commented Jan 20, 2018 at 23:21
  • 2
    There is a new required parameter since QGIS 3.0. See an updated answer here: gis.stackexchange.com/questions/222494/… Commented Jun 26, 2020 at 3:57
6

In QGIS3, the API needs to pass QgsProject as the first argument.

It is: setProjectVariable(project: QgsProject, name: str, value: Any)

QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), 'myvar','hello world')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.