Subgroup: Property

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.
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.
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.

Signals

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

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.

expressionString(self) → str

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

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()

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

Returns a new ExpressionBasedProperty created from the specified expression.

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

Returns a new FieldBasedProperty created from the specified field name.

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

Returns a new StaticProperty created from the specified value.

isActive(self) → bool

Returns whether the property is currently active.

See also

setActive()

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()

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.

propertyType(self) → QgsProperty.Type

Returns the property type.

referencedFields(self, context: QgsExpressionContext = QgsExpressionContext()) → Set[str]

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

setActive(self, active: bool)

Sets whether the property is currently active.

See also

isActive()

setExpressionString(self, expression: str)

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

setField(self, field: str)

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

See also

field()

setStaticValue(self, value: Any)

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

See also

staticValue()

setTransformer(self, transformer: QgsPropertyTransformer)

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

Parameters:transformer – transformer to install. Ownership is transferred to the property, and any

existing transformer will be deleted. Set to null 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()

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()

transformer(self) → QgsPropertyTransformer

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

See also

setTransformer()

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 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. :param defaultValue: default value to return if the property is not active or cannot be calculated :param ok: if specified, will be set to true if conversion was successful

Returns:calculated value for property

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:
  • context – QgsExpressionContext to evaluate the property for.
  • defaultValue – default boolean to return if the property cannot be calculated as an boolean
  • ok – if specified, will be set to true if conversion was successful
Returns:

value parsed to boolean

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:
  • context – QgsExpressionContext to evaluate the property for.
  • defaultColor – default color to return if the property cannot be calculated as a color
  • ok – if specified, will be set to true if conversion was successful
Returns:

value parsed to color

See also

value()

See also

valueAsString()

See also

valueAsDouble()

See also

valueAsInt()

See also

valueAsBool()

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

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

Parameters:
  • context – QgsExpressionContext to evaluate the property for.
  • defaultValue – default double to return if the property cannot be calculated as a double
  • ok – if specified, will be set to true if conversion was successful
Returns:

value parsed to double

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:
  • context – QgsExpressionContext to evaluate the property for.
  • defaultValue – default integer to return if the property cannot be calculated as an integer
  • ok – if specified, will be set to true if conversion was successful
Returns:

value parsed to integer

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:
  • context – QgsExpressionContext to evaluate the property for.
  • defaultString – default string to return if the property cannot be calculated as a string
  • ok – if specified, will be set to true if conversion was successful
Returns:

value parsed to string

See also

value()

See also

valueAsColor()

See also

valueAsDouble()

See also

valueAsInt()

See also

valueAsBool()