QgsExpressionContextScope class

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.

Public types

struct StaticVariable
Single variable definition for use within a QgsExpressionContextScope.

Constructors, destructors, conversion operators

QgsExpressionContextScope(const QString& name = QString())
Constructor for QgsExpressionContextScope.
QgsExpressionContextScope(const QgsExpressionContextScope& other)
Copy constructor.

Public functions

void addFunction(const QString& name, QgsScopedExpressionFunction* function)
Adds a function to the scope.
void addVariable(const QgsExpressionContextScope::StaticVariable& variable)
Adds a variable into the context scope.
auto description(const QString& name) const -> QString
Returns the translated description for the variable with the specified name (if set).
auto feature() const -> QgsFeature
Sets the feature associated with the scope.
auto filteredVariableNames() const -> QStringList
Returns a filtered and sorted list of variable names contained within the scope.
auto function(const QString& name) const -> QgsExpressionFunction*
Retrieves a function from the scope.
auto functionNames() const -> QStringList
Retrieves a list of names of functions contained in the scope.
auto hasFeature() const -> bool
Returns true if the scope has a feature associated with it.
auto hasFunction(const QString& name) const -> bool
Tests whether a function with the specified name exists in the scope.
auto hasVariable(const QString& name) const -> bool
Tests whether a variable with the specified name exists in the scope.
auto isReadOnly(const QString& name) const -> bool
Tests whether the specified variable is read only and should not be editable by users.
auto isStatic(const QString& name) const -> bool
Tests whether the variable with the specified name is static and can be cached.
auto name() const -> QString
Returns the friendly display name of the context scope.
void removeFeature()
Removes any feature associated with the scope.
auto removeVariable(const QString& name) -> bool
Removes a variable from the context scope, if found.
void setFeature(const QgsFeature& feature)
Convenience function for setting a feature for the scope.
void setFields(const QgsFields& fields)
Convenience function for setting a fields for the scope.
void setVariable(const QString& name, const QVariant& value, bool isStatic = false)
Convenience method for setting a variable in the context scope by name name and value.
auto variable(const QString& name) const -> QVariant
Retrieves a variable's value from the scope.
auto variableCount() const -> int
Returns the count of variables contained within the scope.
auto variableNames() const -> QStringList
Returns a list of variable names contained within the scope.

Function documentation

QgsExpressionContextScope::QgsExpressionContextScope(const QString& name = QString())

Constructor for QgsExpressionContextScope.

Parameters
name friendly display name for the context scope

void QgsExpressionContextScope::addFunction(const QString& name, QgsScopedExpressionFunction* function)

Adds a function to the scope.

Parameters
name function name
function function to insert. Ownership is transferred to the scope.

void QgsExpressionContextScope::addVariable(const QgsExpressionContextScope::StaticVariable& variable)

Adds a variable into the context scope.

Parameters
variable definition of variable to insert

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

QString QgsExpressionContextScope::description(const QString& name) const

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

QgsFeature QgsExpressionContextScope::feature() const

Sets the feature associated with the scope.

QStringList QgsExpressionContextScope::filteredVariableNames() const

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.

QgsExpressionFunction* QgsExpressionContextScope::function(const QString& name) const

Retrieves a function from the scope.

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

QStringList QgsExpressionContextScope::functionNames() const

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

bool QgsExpressionContextScope::hasFeature() const

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

bool QgsExpressionContextScope::hasFunction(const QString& name) const

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

bool QgsExpressionContextScope::hasVariable(const QString& name) const

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

bool QgsExpressionContextScope::isReadOnly(const QString& name) const

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

bool QgsExpressionContextScope::isStatic(const QString& name) const

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

void QgsExpressionContextScope::removeFeature()

Removes any feature associated with the scope.

bool QgsExpressionContextScope::removeVariable(const QString& name)

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

void QgsExpressionContextScope::setFeature(const QgsFeature& feature)

Convenience function for setting a feature for the scope.

Parameters
feature feature for scope

Any existing feature set by the scope will be overwritten.

void QgsExpressionContextScope::setFields(const QgsFields& fields)

Convenience function for setting a fields for the scope.

Parameters
fields fields for scope

Any existing fields set by the scope will be overwritten.

void QgsExpressionContextScope::setVariable(const QString& name, const QVariant& value, bool isStatic = 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().

QVariant QgsExpressionContextScope::variable(const QString& name) const

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

QStringList QgsExpressionContextScope::variableNames() const

Returns a list of variable names contained within the scope.