Class: QgsExpressionContextUtils

class qgis.core.QgsExpressionContextUtils

Bases: sip.wrapper

Contains utilities for working with QgsExpressionContext objects, including methods for creating scopes for specific uses (e.g., project scopes, layer scopes).

Methods

atlasScope

Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.

createFeatureBasedContext

Helper function for creating an expression context which contains just a feature and fields collection.

formScope

Creates a new scope which contains functions and variables from the current attribute form/table formFeature.

globalProjectLayerScopes

Creates a list of three scopes: global, layer’s project and layer.

globalScope

Creates a new scope which contains variables and functions relating to the global QGIS context.

layerScope

Creates a new scope which contains variables and functions relating to a QgsMapLayer.

layoutItemScope

Creates a new scope which contains variables and functions relating to a QgsLayoutItem.

layoutScope

Creates a new scope which contains variables and functions relating to a QgsLayout layout.

mapSettingsScope

Creates a new scope which contains variables and functions relating to a QgsMapSettings object.

mapToolCaptureScope

Sets the expression context variables which are available for expressions triggered by a map tool capture like add feature.

multiFrameScope

Creates a new scope which contains variables and functions relating to a QgsLayoutMultiFrame.

notificationScope

Creates a new scope which contains variables and functions relating to provider notifications

parentFormScope

Creates a new scope which contains functions and variables from the current parent attribute form/table formFeature.

processingAlgorithmScope

Creates a new scope which contains variables and functions relating to a processing algorithm, when used with the specified parameters and context.

processingModelAlgorithmScope

Creates a new scope which contains variables and functions relating to a processing model algorithm, when used with the specified parameters and context.

projectScope

Creates a new scope which contains variables and functions relating to a QGIS project.

registerContextFunctions

Registers all known core functions provided by QgsExpressionContextScope objects.

removeGlobalVariable

Remove a global context variable.

removeProjectVariable

Remove project context variable.

setGlobalVariable

Sets a global context variable.

setGlobalVariables

Sets all global context variables.

setLayerVariable

Sets a layer context variable.

setLayerVariables

Sets all layer context variables.

setLayoutItemVariable

Sets a layout item context variable, with the given name and value.

setLayoutItemVariables

Sets all layout item context variables for an item.

setLayoutMultiFrameVariable

Sets a layout multi frame context variable, with the given name and value.

setLayoutMultiFrameVariables

Sets all layout multiframe context variables for an frame.

setLayoutVariable

Sets a layout context variable.

setLayoutVariables

Sets all layout context variables.

setProjectVariable

Sets a project context variable.

setProjectVariables

Sets all project context variables.

updateSymbolScope

Updates a symbol scope related to a QgsSymbol to an expression context.

atlasScope(atlas: QgsLayoutAtlas)QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas. For instance, current page name and number.

Parameters

atlas (QgsLayoutAtlas) – source atlas. If None, a set of default atlas variables will be added to the scope.

Return type

QgsExpressionContextScope

createFeatureBasedContext(feature: QgsFeature, fields: QgsFields)QgsExpressionContext

Helper function for creating an expression context which contains just a feature and fields collection. Generally this method should not be used as the created context does not include standard scopes such as the global and project scopes.

Parameters
Return type

QgsExpressionContext

formScope(formFeature: QgsFeature = QgsFeature(), formMode: str = '')QgsExpressionContextScope

Creates a new scope which contains functions and variables from the current attribute form/table formFeature. The variables and values in this scope will reflect the current state of the form/row being edited. The formMode (SingleEditMode etc.) is passed as text

New in version 3.2.

Parameters
  • formFeature (QgsFeature = QgsFeature()) –

  • formMode (str = '') –

Return type

QgsExpressionContextScope

globalProjectLayerScopes(layer: QgsMapLayer)List[QgsExpressionContextScope]

Creates a list of three scopes: global, layer’s project and layer.

New in version 3.0.

Parameters

layer (QgsMapLayer) –

Return type

List[QgsExpressionContextScope]

globalScope()QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to the global QGIS context. For instance, QGIS version numbers and variables specified through QGIS options.

Return type

QgsExpressionContextScope

layerScope(layer: QgsMapLayer)QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to a QgsMapLayer. For instance, layer name, id and fields.

Parameters

layer (QgsMapLayer) –

Return type

QgsExpressionContextScope

layoutItemScope(item: QgsLayoutItem)QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to a QgsLayoutItem. For instance, item size and position.

New in version 3.0.

Parameters

item (QgsLayoutItem) –

Return type

QgsExpressionContextScope

layoutScope(layout: QgsLayout)QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to a QgsLayout layout. For instance, number of pages and page sizes.

New in version 3.0.

Parameters

layout (QgsLayout) –

Return type

QgsExpressionContextScope

mapSettingsScope(mapSettings: QgsMapSettings)QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to a QgsMapSettings object. For instance, map scale and rotation.

Parameters

mapSettings (QgsMapSettings) –

Return type

QgsExpressionContextScope

mapToolCaptureScope(matches: Iterable[QgsPointLocator.Match])QgsExpressionContextScope

Sets the expression context variables which are available for expressions triggered by a map tool capture like add feature.

New in version 3.0.

Parameters

matches (Iterable[QgsPointLocator.Match]) –

Return type

QgsExpressionContextScope

multiFrameScope(frame: QgsLayoutMultiFrame)QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to a QgsLayoutMultiFrame.

New in version 3.10.

Parameters

frame (QgsLayoutMultiFrame) –

Return type

QgsExpressionContextScope

notificationScope(message: str = '')QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to provider notifications

Parameters

message (str = '') – the notification message

Return type

QgsExpressionContextScope

parentFormScope(formFeature: QgsFeature = QgsFeature(), formMode: str = '')QgsExpressionContextScope

Creates a new scope which contains functions and variables from the current parent attribute form/table formFeature. The variables and values in this scope will reflect the current state of the parent form/row being edited. The formMode (SingleEditMode etc.) is passed as text

New in version 3.14.

Parameters
  • formFeature (QgsFeature = QgsFeature()) –

  • formMode (str = '') –

Return type

QgsExpressionContextScope

processingAlgorithmScope(algorithm: QgsProcessingAlgorithm, parameters: Dict[str, Any], context: QgsProcessingContext)QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to a processing algorithm, when used with the specified parameters and context. For instance, algorithm name and parameter functions.

Parameters
Return type

QgsExpressionContextScope

processingModelAlgorithmScope(model: QgsProcessingModelAlgorithm, parameters: Dict[str, Any], context: QgsProcessingContext)QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to a processing model algorithm, when used with the specified parameters and context. For instance, model name and path variables.

New in version 3.6.

Parameters
Return type

QgsExpressionContextScope

projectScope(project: QgsProject)QgsExpressionContextScope

Creates a new scope which contains variables and functions relating to a QGIS project. For instance, project path and title, and variables specified through the project properties.

Parameters

project (QgsProject) – What project to use

Return type

QgsExpressionContextScope

registerContextFunctions()

Registers all known core functions provided by QgsExpressionContextScope objects.

removeGlobalVariable(name: str)

Remove a global context variable.

Parameters

name (str) – variable name

See also

globalScope()

removeProjectVariable(project: QgsProject, name: str)

Remove project context variable.

Parameters
  • project (QgsProject) – Project to apply changes to

  • name (str) – variable name

See also

projectScope()

setGlobalVariable(name: str, value: Any)

Sets a global context variable. This variable will be contained within scopes retrieved via globalScope().

Parameters
  • name (str) – variable name

  • value (Any) – variable value

See also

globalScope()

setGlobalVariables(variables: Dict[str, Any])

Sets all global context variables. Existing global variables will be removed and replaced with the variables specified.

Parameters

variables (Dict[str) – new set of global variables

See also

globalScope()

setLayerVariable(layer: QgsMapLayer, name: str, value: Any)

Sets a layer context variable. This variable will be contained within scopes retrieved via layerScope().

Parameters
  • layer (QgsMapLayer) – map layer

  • name (str) – variable name

  • value (Any) – variable value

See also

layerScope()

setLayerVariables(layer: QgsMapLayer, variables: Dict[str, Any])

Sets all layer context variables. Existing layer variables will be removed and replaced with the variables specified.

Parameters
  • layer (QgsMapLayer) – map layer

  • variables (Dict[str) – new set of layer variables

See also

layerScope()

setLayoutItemVariable(item: QgsLayoutItem, name: str, value: Any)

Sets a layout item context variable, with the given name and value. This variable will be contained within scopes retrieved via layoutItemScope().

New in version 3.0.

Parameters
setLayoutItemVariables(item: QgsLayoutItem, variables: Dict[str, Any])

Sets all layout item context variables for an item. Existing variables will be removed and replaced with the variables specified.

New in version 3.0.

Parameters
setLayoutMultiFrameVariable(frame: QgsLayoutMultiFrame, name: str, value: Any)

Sets a layout multi frame context variable, with the given name and value. This variable will be contained within scopes retrieved via multiFrameScope().

New in version 3.10.

Parameters
setLayoutMultiFrameVariables(frame: QgsLayoutMultiFrame, variables: Dict[str, Any])

Sets all layout multiframe context variables for an frame. Existing variables will be removed and replaced with the variables specified.

New in version 3.10.

Parameters
setLayoutVariable(layout: QgsLayout, name: str, value: Any)

Sets a layout context variable. This variable will be contained within scopes retrieved via layoutScope().

Parameters
  • layout (QgsLayout) – target layout

  • name (str) – variable name

  • value (Any) – variable value

See also

layoutScope()

New in version 3.0.

setLayoutVariables(layout: QgsLayout, variables: Dict[str, Any])

Sets all layout context variables. Existing layout variables will be removed and replaced with the variables specified.

Parameters
  • layout (QgsLayout) – target layout

  • variables (Dict[str) – new set of layer variables

See also

layoutScope()

New in version 3.0.

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

Sets a project context variable. This variable will be contained within scopes retrieved via projectScope().

Parameters
  • project (QgsProject) – Project to apply changes to

  • name (str) – variable name

  • value (Any) – variable value

See also

projectScope()

setProjectVariables(project: QgsProject, variables: Dict[str, Any])

Sets all project context variables. Existing project variables will be removed and replaced with the variables specified.

Parameters
  • project (QgsProject) – Project to apply changes to

  • variables (Dict[str) – new set of project variables

See also

projectScope()

updateSymbolScope(symbol: QgsSymbol, symbolScope: QgsExpressionContextScope = None)QgsExpressionContextScope

Updates a symbol scope related to a QgsSymbol to an expression context.

Parameters

New in version 2.14.

Return type

QgsExpressionContextScope