Class: QgsExpressionContextScope¶
- class qgis.core.QgsExpressionContextScope¶
Bases:
sip.wrapper
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 withQgsExpressionContextScope
objects.New in version 2.12.
QgsExpressionContextScope(name: str = ‘’) Constructor for QgsExpressionContextScope
- Parameters:
name – friendly display name for the context scope
QgsExpressionContextScope(other:
QgsExpressionContextScope
) Copy constructorMethods
Adds a function to the scope.
Adds the passed variable to a list of hidden variables that won't be visible in the expression builder dialog and widget.
Adds a variable into the context scope.
Returns the translated description for the variable with the specified
name
(if set).Sets the feature associated with the scope.
Returns a filtered and sorted list of variable names contained within the scope.
Retrieves a function from the scope.
Retrieves a list of names of functions contained in the scope.
Sets the geometry associated with the scope.
Returns
True
if the scope has a feature associated with it.Tests whether a function with the specified name exists in the scope.
Returns
True
if the scope has a geometry associated with it.Tests whether a variable with the specified name exists in the scope.
Returns the list of variables hidden within the scope.
Tests whether the specified variable is read only and should not be editable by users.
Tests whether the variable with the specified
name
is static and can be cached.Returns the friendly display name of the context scope.
Reads scope variables from an XML element.
Removes any feature associated with the scope.
Removes any geometry associated with the scope.
Removes the passed variable from a list of hidden variables.
Removes a variable from the context scope, if found.
Convenience function for setting a feature for the scope.
Convenience function for setting a fields for the scope.
Convenience function for setting a
geometry
for the scope.Sets the list of variables intended to be hidden in the expression builder dialog and widget.
Convenience method for setting a variable in the context scope by
name
name andvalue
.Retrieves a variable's value from the scope.
Returns the count of variables contained within the scope.
Returns a list of variable names contained within the scope.
Writes scope variables to an XML
element
.- 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 usersisStatic –
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 (str) – function name
function (QgsScopedExpressionFunction) – function to insert. Ownership is transferred to the scope.
See also
- addHiddenVariable(self, hiddenVariable: str)¶
Adds the passed variable to a list of hidden variables that won’t be visible in the expression builder dialog and widget.
See also
See also
See also
New in version 3.28.
- Parameters:
hiddenVariable (str) –
- 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 (QgsExpressionContextScope.StaticVariable) – definition of variable to insert
See also
See also
- description(self, name: str) str ¶
Returns the translated description for the variable with the specified
name
(if set).New in version 3.0.
- Parameters:
name (str) –
- Return type:
str
- feature(self) QgsFeature ¶
Sets the feature associated with the scope.
See also
See also
New in version 3.0.
- Return type:
- 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
- Return type:
List[str]
- function(self, name: str) QgsExpressionFunction ¶
Retrieves a function from the scope.
- Parameters:
name (str) – function name
- Return type:
- Returns:
function, or
None
if matching function could not be found
See also
See also
See also
- functionNames(self) List[str] ¶
Retrieves a list of names of functions contained in the scope.
See also
See also
- Return type:
List[str]
- geometry(self) QgsGeometry ¶
Sets the geometry associated with the scope.
See also
See also
New in version 3.24.
- Return type:
- hasFeature(self) bool ¶
Returns
True
if the scope has a feature associated with it.See also
New in version 3.0.
- Return type:
bool
- hasFunction(self, name: str) bool ¶
Tests whether a function with the specified name exists in the scope.
- Parameters:
name (str) – function name
- Return type:
bool
- Returns:
True
if matching function was found in the scope
See also
See also
- hasGeometry(self) bool ¶
Returns
True
if the scope has a geometry associated with it.See also
New in version 3.24.
- Return type:
bool
- hasVariable(self, name: str) bool ¶
Tests whether a variable with the specified name exists in the scope.
- Parameters:
name (str) – variable name
- Return type:
bool
- Returns:
True
if matching variable was found in the scope
See also
See also
Returns the list of variables hidden within the scope.
See also
See also
See also
New in version 3.28.
- Return type:
List[str]
- isReadOnly(self, name: str) bool ¶
Tests whether the specified variable is read only and should not be editable by users.
- Parameters:
name (str) – variable name
- Return type:
bool
- 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.
- Parameters:
name (str) –
- Return type:
bool
- name(self) str ¶
Returns the friendly display name of the context scope.
- Return type:
str
- readXml(self, element: QDomElement, context: QgsReadWriteContext)¶
Reads scope variables from an XML element.
See also
New in version 3.6.
- Parameters:
element (QDomElement) –
context (QgsReadWriteContext) –
- removeFeature(self)¶
Removes any feature associated with the scope.
See also
See also
New in version 3.0.
- removeGeometry(self)¶
Removes any geometry associated with the scope.
See also
See also
New in version 3.24.
- removeHiddenVariable(self, hiddenVariable: str)¶
Removes the passed variable from a list of hidden variables.
See also
See also
See also
New in version 3.28.
- Parameters:
hiddenVariable (str) –
- removeVariable(self, name: str) bool ¶
Removes a variable from the context scope, if found.
- Parameters:
name (str) – name of variable to remove
- Return type:
bool
- 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 (QgsFeature) – feature for scope
See also
See also
- 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 (QgsFields) – fields for scope
- setGeometry(self, geometry: QgsGeometry)¶
Convenience function for setting a
geometry
for the scope. Any existing geometry set by the scope will be overwritten.See also
See also
New in version 3.24.
- Parameters:
geometry (QgsGeometry) –
- setHiddenVariables(self, hiddenVariables: Iterable[str])¶
Sets the list of variables intended to be hidden in the expression builder dialog and widget.
See also
See also
See also
New in version 3.28.
- Parameters:
hiddenVariables (Iterable[str]) –
- setVariable(self, name: str, value: Any, isStatic: bool = False)¶
Convenience method for setting a variable in the context scope by
name
name andvalue
. 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 theisStatic
parameter is set toTrue
, this variable can be cached during the execution ofQgsExpression.prepare()
.See also
- Parameters:
name (str) –
value (Any) –
isStatic (bool = False) –
- variable(self, name: str) Any ¶
Retrieves a variable’s value from the scope.
- Parameters:
name (str) – variable name
- Return type:
Any
- Returns:
variable value, or invalid QVariant if matching variable could not be found
See also
See also
- variableCount(self) int ¶
Returns the count of variables contained within the scope.
- Return type:
int
- variableNames(self) List[str] ¶
Returns a list of variable names contained within the scope.
See also
See also
- Return type:
List[str]
- writeXml(self, element: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool ¶
Writes scope variables to an XML
element
.See also
New in version 3.6.
- Parameters:
element (QDomElement) –
document (QDomDocument) –
context (QgsReadWriteContext) –
- Return type:
bool