Class: QgsProperty

class qgis.core.QgsProperty

Bases: sip.wrapper

Constructor for a QgsAbstractProperty. The property will be set to an InvalidProperty type.

QgsProperty(other: QgsProperty) Copy constructor

A store for object properties.

QgsProperty objects are used for storing properties for objects, which can then be transformed to a QVariant value by evaluating them against a supplied QgsExpressionContext. Multiple QgsProperty objects can be grouped using a QgsPropertyCollection for easier bulk storage, retrieval and evaluation.

QgsProperty objects are implicitly shared and can be inexpensively copied.

New in version 3.0:

Methods

asExpression

Returns an expression string representing the state of the property, or an empty string if the property could not be converted to an expression

convertToTransformer

Attempts to convert an existing expression based property to a base expression with corresponding transformer.

expressionString

Returns the expression used for the property value.

field

Returns the current field name the property references.

fromExpression

Returns a new ExpressionBasedProperty created from the specified expression.

fromField

Returns a new FieldBasedProperty created from the specified field name.

fromValue

Returns a new StaticProperty created from the specified value.

isActive

Returns whether the property is currently active.

isProjectColor

Returns True if the property is set to a linked project color.

loadVariant

Loads this property from a QVariantMap, wrapped in a QVariant.

prepare

Prepares the property against a specified expression context.

propertyType

Returns the property type.

referencedFields

Returns the set of any fields referenced by the property for a specified expression context.

setActive

Sets whether the property is currently active.

setExpressionString

Sets the expression to use for the property value.

setField

Sets the field name the property references.

setStaticValue

Sets the static value for the property.

setTransformer

Sets an optional transformer to use for manipulating the calculated values for the property.

staticValue

Returns the current static value for the property.

toVariant

Saves this property to a QVariantMap, wrapped in a QVariant.

transformer

Returns the existing transformer used for manipulating the calculated values for the property, if set.

value

Calculates the current value of the property, including any transforms which are set for the property

valueAsBool

Calculates the current value of the property and interprets it as an boolean.

valueAsColor

Calculates the current value of the property and interprets it as a color.

valueAsDateTime

Calculates the current value of the property and interprets it as a datetime.

valueAsDouble

Calculates the current value of the property and interprets it as a double.

valueAsInt

Calculates the current value of the property and interprets it as an integer.

valueAsString

Calculates the current value of the property and interprets it as a string.

Attributes

ExpressionBasedProperty

FieldBasedProperty

InvalidProperty

StaticProperty

ExpressionBasedProperty = 3
FieldBasedProperty = 2
InvalidProperty = 0
StaticProperty = 1
class Type

Bases: int

asExpression(self) str

Returns an expression string representing the state of the property, or an empty string if the property could not be converted to an expression

Return type

str

convertToTransformer(self) bool

Attempts to convert an existing expression based property to a base expression with corresponding transformer. Returns True if conversion was successful. Note that calling this method requires multiple parsing of expressions, so it should only be called in non-performance critical code.

Return type

bool

expressionString(self) str

Returns the expression used for the property value. If the property is not a ExpressionBasedProperty this will return an empty string.

Return type

str

field(self) str

Returns the current field name the property references. If the property is not a FieldBasedProperty this will return an empty string.

See also

setField()

Return type

str

fromExpression(expression: str, isActive: bool = True) QgsProperty

Returns a new ExpressionBasedProperty created from the specified expression.

Parameters
  • expression (str) –

  • isActive (bool = True) –

Return type

QgsProperty

fromField(fieldName: str, isActive: bool = True) QgsProperty

Returns a new FieldBasedProperty created from the specified field name.

Parameters
  • fieldName (str) –

  • isActive (bool = True) –

Return type

QgsProperty

fromValue(value: Any, isActive: bool = True) QgsProperty

Returns a new StaticProperty created from the specified value.

Parameters
  • value (Any) –

  • isActive (bool = True) –

Return type

QgsProperty

isActive(self) bool

Returns whether the property is currently active.

See also

setActive()

Return type

bool

isProjectColor(self) bool

Returns True if the property is set to a linked project color.

New in version 3.6.

Return type

bool

loadVariant(self, property: Any) bool

Loads this property from a QVariantMap, wrapped in a QVariant. You can use QgsXmlUtils.readVariant to load it from an XML document.

See also

toVariant()

Parameters

property (Any) –

Return type

bool

prepare(self, context: QgsExpressionContext = QgsExpressionContext()) bool

Prepares the property against a specified expression context. Calling prepare before evaluating the property multiple times allows precalculation of expensive setup tasks such as parsing expressions. Returns True if preparation was successful.

Parameters

context (QgsExpressionContext = QgsExpressionContext()) –

Return type

bool

propertyType(self) QgsProperty.Type

Returns the property type.

Return type

QgsProperty.Type

referencedFields(self, context: QgsExpressionContext = QgsExpressionContext(), ignoreContext: bool = False) Set[str]

Returns the set of any fields referenced by the property for a specified expression context.

Note

The optional argument ignoreContext has been added in QGIS 3.14. When set to True, even fields not set in context’s fields() will be reported - this is useful e.g. with vector tiles where the actual available field names may not be known beforehand.

Parameters
Return type

Set[str]

setActive(self, active: bool)

Sets whether the property is currently active.

See also

isActive()

Parameters

active (bool) –

setExpressionString(self, expression: str)

Sets the expression to use for the property value. Calling this will transform the property into an ExpressionBasedProperty.

Parameters

expression (str) –

setField(self, field: str)

Sets the field name the property references. Calling this will transform the property into an FieldBasedProperty.

See also

field()

Parameters

field (str) –

setStaticValue(self, value: Any)

Sets the static value for the property. Calling this will transform the property into an StaticProperty.

See also

staticValue()

Parameters

value (Any) –

setTransformer(self, transformer: QgsPropertyTransformer)

Sets an optional transformer to use for manipulating the calculated values for the property.

Parameters

transformer (QgsPropertyTransformer) – transformer to install. Ownership is transferred to the property, and any existing transformer will be deleted. Set to None to remove an existing transformer.

See also

transformer()

staticValue(self) Any

Returns the current static value for the property. If the property is not a StaticProperty this will return an invalid variant.

See also

setStaticValue()

Return type

Any

toVariant(self) Any

Saves this property to a QVariantMap, wrapped in a QVariant. You can use QgsXmlUtils.writeVariant to save it to an XML document.

See also

loadVariant()

Return type

Any

transformer(self) QgsPropertyTransformer

Returns the existing transformer used for manipulating the calculated values for the property, if set.

See also

setTransformer()

Return type

QgsPropertyTransformer

value(self, context: QgsExpressionContext, defaultValue: Any = None) Tuple[Any, bool]

Calculates the current value of the property, including any transforms which are set for the property

Parameters
  • context (QgsExpressionContext) – QgsExpressionContext to evaluate the property for. The variables and functions contained in the expression context can be used to alter the calculated value for the property, so that a property is able to respond to the current environment, layers and features within QGIS.

  • defaultValue (Any = None) – default value to return if the property is not active or cannot be calculated

Return type

Tuple[Any, bool]

Returns

  • calculated value for property

  • ok: will be set to True if conversion was successful

See also

valueAsString()

See also

valueAsColor()

See also

valueAsDouble()

See also

valueAsInt()

See also

valueAsBool()

valueAsBool(self, context: QgsExpressionContext, defaultValue: bool = False) Tuple[bool, bool]

Calculates the current value of the property and interprets it as an boolean.

Parameters
Return type

Tuple[bool, bool]

Returns

  • value parsed to boolean

  • ok: will be set to True if conversion was successful

See also

value()

See also

valueAsString()

See also

valueAsColor()

See also

valueAsDouble()

See also

valueAsInt()

valueAsColor(self, context: QgsExpressionContext, defaultColor: Union[QColor, Qt.GlobalColor, QGradient] = QColor()) Tuple[QColor, bool]

Calculates the current value of the property and interprets it as a color.

Parameters
Return type

Tuple[QColor, bool]

Returns

  • value parsed to color

  • ok: will be set to True if conversion was successful

See also

value()

See also

valueAsString()

See also

valueAsDouble()

See also

valueAsInt()

See also

valueAsBool()

valueAsDateTime(self, context: QgsExpressionContext, defaultDateTime: Union[QDateTime, datetime.datetime] = QDateTime()) Tuple[QDateTime, bool]

Calculates the current value of the property and interprets it as a datetime.

Parameters
  • context (QgsExpressionContext) – QgsExpressionContext to evaluate the property for.

  • defaultDateTime (Union[QDateTime) – default datetime to return if the property cannot be calculated as a datetime

Return type

Tuple[QDateTime, bool]

Returns

  • value parsed to datetime

  • ok: will be set to True if conversion was successful

See also

value()

See also

valueAsString()

See also

valueAsColor()

See also

valueAsDouble()

See also

valueAsInt()

See also

valueAsBool()

New in version 3.14.

valueAsDouble(self, context: QgsExpressionContext, defaultValue: float = 0) Tuple[float, bool]

Calculates the current value of the property and interprets it as a double.

Parameters
Return type

Tuple[float, bool]

Returns

  • value parsed to double

  • ok: will be set to True if conversion was successful

See also

value()

See also

valueAsString()

See also

valueAsColor()

See also

valueAsInt()

See also

valueAsBool()

valueAsInt(self, context: QgsExpressionContext, defaultValue: int = 0) Tuple[int, bool]

Calculates the current value of the property and interprets it as an integer.

Parameters
Return type

Tuple[int, bool]

Returns

  • value parsed to integer

  • ok: will be set to True if conversion was successful

See also

value()

See also

valueAsString()

See also

valueAsColor()

See also

valueAsDouble()

See also

valueAsBool()

valueAsString(self, context: QgsExpressionContext, defaultString: str = '') Tuple[str, bool]

Calculates the current value of the property and interprets it as a string.

Parameters
Return type

Tuple[str, bool]

Returns

  • value parsed to string

  • ok: will be set to True if conversion was successful

See also

value()

See also

valueAsColor()

See also

valueAsDouble()

See also

valueAsInt()

See also

valueAsBool()