[QGIS-Developer] Remove layer variable
Nyall Dawson
nyall.dawson at gmail.com
Wed Jun 4 16:23:11 PDT 2025
On Mon, 2 Jun 2025 at 19:16, Simon Gröchenig via QGIS-Developer
<qgis-developer at lists.osgeo.org> wrote:
>
> Hi,
>
> how can I remove a layer variable with Python?
>
> The following returns true, but does not work.
>
> QgsExpressionContextUtils.layerScope(layer).removeVariable("variable_name")
That's cos it's just removing the variable from a copy of the layer's
expression context scope, not from the layer itself.
It's rather gross, but try
https://qgis.org/pyqgis/3.40/core/QgsExpressionContextUtils.html#qgis.core.QgsExpressionContextUtils.setLayerVariables
-- you'll need to first retrieve the existing variables via
https://qgis.org/pyqgis/3.40/core/QgsExpressionContextUtils.html#qgis.core.QgsExpressionContextUtils.layerScope
and then build a dictionary of the variables you want to keep by
iterating over https://qgis.org/pyqgis/3.40/core/QgsExpressionContextScope.html#qgis.core.QgsExpressionContextScope.variableNames
Hope that helps!
Nyall
>
> Best regards
> Simon
>
> _____________________
> Simon Gröchenig
> GIS Support und Training
> Treffling 73 - 9871 Seeboden - Österreich
> Tel.: +43 699 11 22 31 22
> Web: https://www.sgroe.at
> E-Mail: gis at sgroe.at
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
More information about the QGIS-Developer
mailing list