Class: QgsExpressionContextScope

class qgis.core.QgsExpressionContextScope(name: str = '')

Bases: sip.wrapper

Constructor for QgsExpressionContextScope

Parameters:name – friendly display name for the context scope

QgsExpressionContextScope(other: QgsExpressionContextScope) Copy constructor

Single scope for storing variables and functions for use within a QgsExpressionContext. Examples include a project’s scope, which could contain information about the current project such as the project file’s location. QgsExpressionContextScope can encapsulate both variables (static values) and functions(which are calculated only when an expression is evaluated).

See QgsExpressionContextUtils for helper methods for working with QgsExpressionContextScope objects.

New in version 2.12: Methods

addFunction Adds a function to the scope.
addVariable Adds a variable into the context scope.
description Returns the translated description for the variable with the specified name (if set).
feature Sets the feature associated with the scope.
filteredVariableNames Returns a filtered and sorted list of variable names contained within the scope.
function Retrieves a function from the scope.
functionNames Retrieves a list of names of functions contained in the scope.
hasFeature Returns true if the scope has a feature associated with it.
hasFunction Tests whether a function with the specified name exists in the scope.
hasVariable Tests whether a variable with the specified name exists in the scope.
isReadOnly Tests whether the specified variable is read only and should not be editable by users.
isStatic Tests whether the variable with the specified name is static and can be cached.
name Returns the friendly display name of the context scope.
readXml Reads scope variables from an XML element.
removeFeature Removes any feature associated with the scope.
removeVariable Removes a variable from the context scope, if found.
setFeature Convenience function for setting a feature for the scope.
setFields Convenience function for setting a fields for the scope.
setVariable Convenience method for setting a variable in the context scope by name name and value.
variable Retrieves a variable’s value from the scope.
variableCount Returns the count of variables contained within the scope.
variableNames Returns a list of variable names contained within the scope.
writeXml Writes scope variables to an XML element.

Signals

Attributes

class StaticVariable(name: str = '', value: Any = None, readOnly: bool = False, isStatic: bool = False, description: str = '')

Bases: sip.wrapper

Constructor for StaticVariable.

Parameters:
  • name – variable name (should be unique within the QgsExpressionContextScope)
  • value – initial variable value
  • readOnly – true if variable should not be editable by users
  • isStatic – true if the variable will not change during the lifteime of an iterator.
  • description – optional translated description of variable, for use in expression builder widgets

QgsExpressionContextScope.StaticVariable(QgsExpressionContextScope.StaticVariable)

description
isStatic
name
readOnly
value
addFunction(self, name: str, function: QgsScopedExpressionFunction)

Adds a function to the scope.

Parameters:
  • name – function name
  • function – function to insert. Ownership is transferred to the scope.

See also

addVariable()

addVariable(self, variable: QgsExpressionContextScope.StaticVariable)

Adds a variable into the context scope. If a variable with the same name is already set then its value is overwritten, otherwise a new variable is added to the scope.

Parameters:variable – definition of variable to insert

See also

setVariable()

See also

addFunction()

description(self, name: str) → str

Returns the translated description for the variable with the specified name (if set).

New in version 3.0.

feature(self) → QgsFeature

Sets the feature associated with the scope.

See also

setFeature()

See also

hasFeature()

New in version 3.0.

filteredVariableNames(self) → List[str]

Returns a filtered and sorted list of variable names contained within the scope. Hidden variable names will be excluded, and the list will be sorted so that read only variables are listed first.

See also

variableNames()

function(self, name: str) → QgsExpressionFunction

Retrieves a function from the scope.

Parameters:name – function name
Returns:function, or null if matching function could not be found

See also

hasFunction()

See also

functionNames()

See also

variable()

functionNames(self) → List[str]

Retrieves a list of names of functions contained in the scope.

See also

function()

See also

variableNames()

hasFeature(self) → bool

Returns true if the scope has a feature associated with it.

See also

feature()

New in version 3.0.

hasFunction(self, name: str) → bool

Tests whether a function with the specified name exists in the scope.

Parameters:name – function name
Returns:true if matching function was found in the scope

See also

function()

See also

hasFunction()

hasVariable(self, name: str) → bool

Tests whether a variable with the specified name exists in the scope.

Parameters:name – variable name
Returns:true if matching variable was found in the scope

See also

variable()

See also

hasFunction()

isReadOnly(self, name: str) → bool

Tests whether the specified variable is read only and should not be editable by users.

Parameters:name – variable name
Returns:true if variable is read only
isStatic(self, name: str) → bool

Tests whether the variable with the specified name is static and can be cached.

New in version 3.0.

name(self) → str

Returns the friendly display name of the context scope.

readXml(self, element: QDomElement, context: QgsReadWriteContext)

Reads scope variables from an XML element.

See also

writeXml()

New in version 3.6.

removeFeature(self)

Removes any feature associated with the scope.

See also

setFeature()

See also

hasFeature()

New in version 3.0.

removeVariable(self, name: str) → bool

Removes a variable from the context scope, if found.

Parameters:name – name of variable to remove
Returns:true if variable was removed from the scope, false if matching variable was not found within the scope
setFeature(self, feature: QgsFeature)

Convenience function for setting a feature for the scope. Any existing feature set by the scope will be overwritten.

Parameters:feature – feature for scope

See also

removeFeature()

See also

feature()

setFields(self, fields: QgsFields)

Convenience function for setting a fields for the scope. Any existing fields set by the scope will be overwritten.

Parameters:fields – fields for scope
setVariable(self, name: str, value: Any, isStatic: bool = False)

Convenience method for setting a variable in the context scope by name name and value. If a variable with the same name is already set then its value is overwritten, otherwise a new variable is added to the scope. If the isStatic parameter is set to true, this variable can be cached during the execution of QgsExpression.prepare()

See also

addVariable()

variable(self, name: str) → Any

Retrieves a variable’s value from the scope.

Parameters:name – variable name
Returns:variable value, or invalid QVariant if matching variable could not be found

See also

hasVariable()

See also

function()

variableCount(self) → int

Returns the count of variables contained within the scope.

variableNames(self) → List[str]

Returns a list of variable names contained within the scope.

See also

functionNames()

writeXml(self, element: QDomElement, document: QDomDocument, context: QgsReadWriteContext) → bool

Writes scope variables to an XML element.

See also

readXml()

New in version 3.6.