QgsExpressionContextUtils class

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

Contents

Public static functions

static auto atlasScope(QgsLayoutAtlas* atlas) -> QgsExpressionContextScope*
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
static auto createFeatureBasedContext(const QgsFeature& feature, const QgsFields& fields) -> QgsExpressionContext
Helper function for creating an expression context which contains just a feature and fields collection.
static auto formScope(const QgsFeature& formFeature = QgsFeature(), const QString& formMode = QString()) -> QgsExpressionContextScope*
Creates a new scope which contains functions and variables from the current attribute form/table feature.
static auto globalProjectLayerScopes(const QgsMapLayer* layer) -> QList<QgsExpressionContextScope*>
Creates a list of three scopes: global, layer's project and layer.
static auto globalScope() -> QgsExpressionContextScope*
Creates a new scope which contains variables and functions relating to the global QGIS context.
static auto layerScope(const QgsMapLayer* layer) -> QgsExpressionContextScope*
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static auto layoutItemScope(const QgsLayoutItem* item) -> QgsExpressionContextScope*
Creates a new scope which contains variables and functions relating to a QgsLayoutItem.
static auto layoutScope(const QgsLayout* layout) -> QgsExpressionContextScope*
Creates a new scope which contains variables and functions relating to a QgsLayout layout.
static auto mapSettingsScope(const QgsMapSettings& mapSettings) -> QgsExpressionContextScope*
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
static auto mapToolCaptureScope(const QList<QgsPointLocator::Match>& matches) -> QgsExpressionContextScope*
Sets the expression context variables which are available for expressions triggered by a map tool capture like add feature.
static auto notificationScope(const QString& message = QString()) -> QgsExpressionContextScope*
Creates a new scope which contains variables and functions relating to provider notifications.
static auto processingAlgorithmScope(const QgsProcessingAlgorithm* algorithm, const QVariantMap& parameters, QgsProcessingContext& context) -> QgsExpressionContextScope*
Creates a new scope which contains variables and functions relating to a processing algorithm, when used with the specified parameters and context.
static auto projectScope(const QgsProject* project) -> QgsExpressionContextScope*
Creates a new scope which contains variables and functions relating to a QGIS project.
static void registerContextFunctions()
Registers all known core functions provided by QgsExpressionContextScope objects.
static void removeGlobalVariable(const QString& name)
Remove a global context variable.
static void removeProjectVariable(QgsProject* project, const QString& name)
Remove project context variable.
static void setGlobalVariable(const QString& name, const QVariant& value)
Sets a global context variable.
static void setGlobalVariables(const QVariantMap& variables)
Sets all global context variables.
static void setLayerVariable(QgsMapLayer* layer, const QString& name, const QVariant& value)
Sets a layer context variable.
static void setLayerVariables(QgsMapLayer* layer, const QVariantMap& variables)
Sets all layer context variables.
static void setLayoutItemVariable(QgsLayoutItem* item, const QString& name, const QVariant& value)
Sets a layout item context variable, with the given name and value.
static void setLayoutItemVariables(QgsLayoutItem* item, const QVariantMap& variables)
Sets all layout item context variables for an item.
static void setLayoutVariable(QgsLayout* layout, const QString& name, const QVariant& value)
Sets a layout context variable.
static void setLayoutVariables(QgsLayout* layout, const QVariantMap& variables)
Sets all layout context variables.
static void setProjectVariable(QgsProject* project, const QString& name, const QVariant& value)
Sets a project context variable.
static void setProjectVariables(QgsProject* project, const QVariantMap& variables)
Sets all project context variables.
static auto updateSymbolScope(const QgsSymbol* symbol, QgsExpressionContextScope* symbolScope = nullptr) -> QgsExpressionContextScope*
Updates a symbol scope related to a QgsSymbol to an expression context.

Function documentation

static QgsExpressionContextScope* QgsExpressionContextUtils::atlasScope(QgsLayoutAtlas* atlas)

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

Parameters
atlas source atlas. If null, a set of default atlas variables will be added to the scope.

For instance, current page name and number.

static QgsExpressionContext QgsExpressionContextUtils::createFeatureBasedContext(const QgsFeature& feature, const QgsFields& fields)

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.

static QgsExpressionContextScope* QgsExpressionContextUtils::formScope(const QgsFeature& formFeature = QgsFeature(), const QString& formMode = QString())

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

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

static QList<QgsExpressionContextScope*> QgsExpressionContextUtils::globalProjectLayerScopes(const QgsMapLayer* layer)

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

static QgsExpressionContextScope* QgsExpressionContextUtils::globalScope()

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.

static QgsExpressionContextScope* QgsExpressionContextUtils::layerScope(const QgsMapLayer* layer)

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

For instance, layer name, id and fields.

static QgsExpressionContextScope* QgsExpressionContextUtils::layoutItemScope(const QgsLayoutItem* item)

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

For instance, item size and position.

static QgsExpressionContextScope* QgsExpressionContextUtils::layoutScope(const QgsLayout* layout)

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

For instance, number of pages and page sizes.

static QgsExpressionContextScope* QgsExpressionContextUtils::mapSettingsScope(const QgsMapSettings& mapSettings)

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

For instance, map scale and rotation.

static QgsExpressionContextScope* QgsExpressionContextUtils::mapToolCaptureScope(const QList<QgsPointLocator::Match>& matches)

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

static QgsExpressionContextScope* QgsExpressionContextUtils::notificationScope(const QString& message = QString())

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

Parameters
message the notification message

static QgsExpressionContextScope* QgsExpressionContextUtils::processingAlgorithmScope(const QgsProcessingAlgorithm* algorithm, const QVariantMap& parameters, QgsProcessingContext& context)

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.

static QgsExpressionContextScope* QgsExpressionContextUtils::projectScope(const QgsProject* project)

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

Parameters
project What project to use

For instance, project path and title, and variables specified through the project properties.

static void QgsExpressionContextUtils::removeGlobalVariable(const QString& name)

Remove a global context variable.

Parameters
name variable name

static void QgsExpressionContextUtils::removeProjectVariable(QgsProject* project, const QString& name)

Remove project context variable.

Parameters
project Project to apply changes to
name variable name

static void QgsExpressionContextUtils::setGlobalVariable(const QString& name, const QVariant& value)

Sets a global context variable.

Parameters
name variable name
value variable value

This variable will be contained within scopes retrieved via globalScope().

static void QgsExpressionContextUtils::setGlobalVariables(const QVariantMap& variables)

Sets all global context variables.

Parameters
variables new set of global variables

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

static void QgsExpressionContextUtils::setLayerVariable(QgsMapLayer* layer, const QString& name, const QVariant& value)

Sets a layer context variable.

Parameters
layer map layer
name variable name
value variable value

This variable will be contained within scopes retrieved via layerScope().

static void QgsExpressionContextUtils::setLayerVariables(QgsMapLayer* layer, const QVariantMap& variables)

Sets all layer context variables.

Parameters
layer map layer
variables new set of layer variables

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

static void QgsExpressionContextUtils::setLayoutItemVariable(QgsLayoutItem* item, const QString& name, const QVariant& value)

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

This variable will be contained within scopes retrieved via layoutItemScope().

static void QgsExpressionContextUtils::setLayoutItemVariables(QgsLayoutItem* item, const QVariantMap& variables)

Sets all layout item context variables for an item.

Existing variables will be removed and replaced with the variables specified.

static void QgsExpressionContextUtils::setLayoutVariable(QgsLayout* layout, const QString& name, const QVariant& value)

Sets a layout context variable.

Parameters
layout target layout
name variable name
value variable value

This variable will be contained within scopes retrieved via layoutScope().

static void QgsExpressionContextUtils::setLayoutVariables(QgsLayout* layout, const QVariantMap& variables)

Sets all layout context variables.

Parameters
layout target layout
variables new set of layer variables

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

static void QgsExpressionContextUtils::setProjectVariable(QgsProject* project, const QString& name, const QVariant& value)

Sets a project context variable.

Parameters
project Project to apply changes to
name variable name
value variable value

This variable will be contained within scopes retrieved via projectScope().

static void QgsExpressionContextUtils::setProjectVariables(QgsProject* project, const QVariantMap& variables)

Sets all project context variables.

Parameters
project Project to apply changes to
variables new set of project variables

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

static QgsExpressionContextScope* QgsExpressionContextUtils::updateSymbolScope(const QgsSymbol* symbol, QgsExpressionContextScope* symbolScope = nullptr)

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

Parameters
symbol symbol to extract properties from
symbolScope pointer to an existing scope to update