Class: QgsProperty

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.

class qgis.core.QgsProperty[source]

Bases: object

__init__()

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

__init__(other: QgsProperty)
Parameters:

other (QgsProperty)

Type

alias of PropertyType

asExpression(self) str[source]

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[source]

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[source]

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[source]

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

static fromExpression(expression: str | None, isActive: bool = True) QgsProperty[source]

Returns a new ExpressionBasedProperty created from the specified expression.

Parameters:
  • expression (Optional[str])

  • isActive (bool = True)

Return type:

QgsProperty

static fromField(fieldName: str | None, isActive: bool = True) QgsProperty[source]

Returns a new FieldBasedProperty created from the specified field name.

Parameters:
  • fieldName (Optional[str])

  • isActive (bool = True)

Return type:

QgsProperty

static fromValue(value: Any, isActive: bool = True) QgsProperty[source]

Returns a new StaticProperty created from the specified value.

Parameters:
  • value (Any)

  • isActive (bool = True)

Return type:

QgsProperty

isActive(self) bool[source]

Returns whether the property is currently active.

See also

setActive()

Return type:

bool

isProjectColor(self) bool[source]

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

Added in version 3.6.

Return type:

bool

isStaticValueInContext(self, context: QgsExpressionContext)[source]

Returns True if the property is effectively a static value in the specified context.

I.e. if the property type is QgsProperty.ExpressionBasedProperty with a fixed value expression (‘some static value’), this method will return True.

Parameters:

context (QgsExpressionContext) -> (bool) – expression context

Returns:

  • True if property is a static value

  • staticValue: evaluated static value if property is effectively a static value

Added in version 3.24.

loadVariant(self, property: Any) bool[source]

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[source]

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

static propertyMapToVariantMap(propertyMap: Dict[str | None, QgsProperty]) Dict[str, Any][source]

Convert a map of QgsProperty to a map of QVariant This is useful to save a map of properties

Added in version 3.20.

Parameters:

propertyMap (Dict[Optional[str], QgsProperty])

Return type:

Dict[str, Any]

propertyType(self) Qgis.PropertyType[source]

Returns the property type.

Return type:

Qgis.PropertyType

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)[source]

Sets whether the property is currently active.

See also

isActive()

Parameters:

active (bool)

setExpressionString(self, expression: str | None)[source]

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

Parameters:

expression (Optional[str])

setField(self, field: str | None)[source]

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

See also

field()

Parameters:

field (Optional[str])

setStaticValue(self, value: Any)[source]

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 | None)[source]

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

Parameters:

transformer (Optional[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[source]

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[source]

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 | None[source]

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

See also

setTransformer()

Return type:

Optional[QgsPropertyTransformer]

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

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) -> (Any) – default value to return if the property is not active or cannot be calculated

Returns:

  • calculated value for property

  • ok: 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)[source]

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

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

  • defaultValue (bool = False) -> (bool) – default boolean to return if the property cannot be calculated as an boolean

Returns:

  • value parsed to boolean

  • ok: 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: QColor | Qt.GlobalColor = QColor())[source]

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

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

  • defaultColor (Union[QColor, Qt.GlobalColor] = QColor()) -> (QColor) – default color to return if the property cannot be calculated as a color

Returns:

  • value parsed to color

  • ok: 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: QDateTime | datetime.datetime = QDateTime())[source]

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, datetime.datetime] = QDateTime()) -> (QDateTime) – default datetime to return if the property cannot be calculated as a datetime

Returns:

  • value parsed to datetime

  • ok: True if conversion was successful

See also

value()

See also

valueAsString()

See also

valueAsColor()

See also

valueAsDouble()

See also

valueAsInt()

See also

valueAsBool()

Added in version 3.14.

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

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

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

  • defaultValue (float = 0) -> (float) – default double to return if the property cannot be calculated as a double

Returns:

  • value parsed to double

  • ok: 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)[source]

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

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

  • defaultValue (int = 0) -> (int) – default integer to return if the property cannot be calculated as an integer

Returns:

  • value parsed to integer

  • ok: 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 | None = '')[source]

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

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

  • defaultString (Optional[str] = '') -> (str) – default string to return if the property cannot be calculated as a string

Returns:

  • value parsed to string

  • ok: True if conversion was successful

See also

value()

See also

valueAsColor()

See also

valueAsDouble()

See also

valueAsInt()

See also

valueAsBool()

static variantMapToPropertyMap(variantMap: Dict[str, Any]) Dict[str, QgsProperty]

Convert a map of QVariant to a map of QgsProperty This is useful to restore a map of properties. The properties are created using QgsProperty.loadVariant

Added in version 3.20.

Parameters:

variantMap (Dict[str, Any])

Return type:

Dict[str, QgsProperty]