Class: QgsProperty¶
- class qgis.core.QgsProperty¶
Bases:
sip.wrapper
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 suppliedQgsExpressionContext
. MultipleQgsProperty
objects can be grouped using aQgsPropertyCollection
for easier bulk storage, retrieval and evaluation.QgsProperty
objects are implicitly shared and can be inexpensively copied.New in version 3.0.
QgsProperty() Constructor for a
QgsProperty
. The property will be set to an InvalidProperty type.QgsProperty(other:
QgsProperty
) Copy constructorMethods
Returns an expression string representing the state of the property, or an empty string if the property could not be converted to an expression
Attempts to convert an existing expression based property to a base expression with corresponding transformer.
Returns the expression used for the property value.
Returns the current field name the property references.
Returns a new ExpressionBasedProperty created from the specified expression.
Returns a new FieldBasedProperty created from the specified field name.
Returns a new StaticProperty created from the specified value.
Returns whether the property is currently active.
Returns
True
if the property is set to a linked project color.Loads this property from a QVariantMap, wrapped in a QVariant.
Prepares the property against a specified expression context.
Convert a map of
QgsProperty
to a map of QVariant This is useful to save a map of propertiesReturns the property type.
Returns the set of any fields referenced by the property for a specified expression context.
Sets whether the property is currently active.
Sets the expression to use for the property value.
Sets the field name the property references.
Sets the static value for the property.
Sets an optional transformer to use for manipulating the calculated values for the property.
Returns the current static value for the property.
Saves this property to a QVariantMap, wrapped in a QVariant.
Returns the existing transformer used for manipulating the calculated values for the property, if set.
Calculates the current value of the property, including any transforms which are set for the property
Calculates the current value of the property and interprets it as an boolean.
Calculates the current value of the property and interprets it as a color.
Calculates the current value of the property and interprets it as a datetime.
Calculates the current value of the property and interprets it as a double.
Calculates the current value of the property and interprets it as an integer.
Calculates the current value of the property and interprets it as a string.
Convert a map of QVariant to a map of QgsProperty This is useful to restore a map of properties.
Attributes
- 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.
See also
- 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
- 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
- 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
- fromValue(value: Any, isActive: bool = True) QgsProperty ¶
Returns a new StaticProperty created from the specified value.
- Parameters
value (Any) –
isActive (bool = True) –
- Return type
- isActive(self) bool ¶
Returns whether the property is currently active.
See also
- 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
- 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
- propertyMapToVariantMap(propertyMap: Dict[str, QgsProperty]) Dict[str, Any] ¶
Convert a map of
QgsProperty
to a map of QVariant This is useful to save a map of propertiesNew in version 3.20.
- Parameters
propertyMap (Dict[str) –
- Return type
Dict[str, Any]
- propertyType(self) QgsProperty.Type ¶
Returns the property type.
- Return 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’sfields()
will be reported - this is useful e.g. with vector tiles where the actual available field names may not be known beforehand.- Parameters
context (
QgsExpressionContext
= QgsExpressionContext()) –ignoreContext (bool = False) –
- Return type
Set[str]
- setActive(self, active: bool)¶
Sets whether the property is currently active.
See also
- 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.
See also
- 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
- 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
- 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
- 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
- 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
- Return type
Any
- transformer(self) QgsPropertyTransformer ¶
Returns the existing transformer used for manipulating the calculated values for the property, if set.
See also
- Return type
- 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
See also
See also
See also
See also
- 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) –
QgsExpressionContext
to evaluate the property for.defaultValue (bool = False) – default boolean to return if the property cannot be calculated as an boolean
- Return type
Tuple[bool, bool]
- Returns
value parsed to boolean
ok: will be set to
True
if conversion was successful
See also
See also
See also
See also
See also
See also
- 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) –
QgsExpressionContext
to evaluate the property for.defaultColor (Union[QColor) – default color to return if the property cannot be calculated as a color
- Return type
Tuple[QColor, bool]
- Returns
value parsed to color
ok: will be set to
True
if conversion was successful
See also
See also
See also
See also
See also
See also
- 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
See also
See also
See also
See also
See also
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
context (QgsExpressionContext) –
QgsExpressionContext
to evaluate the property for.defaultValue (float = 0) – default double to return if the property cannot be calculated as a double
- Return type
Tuple[float, bool]
- Returns
value parsed to double
ok: will be set to
True
if conversion was successful
See also
See also
See also
See also
See also
See also
- 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) –
QgsExpressionContext
to evaluate the property for.defaultValue (int = 0) – default integer to return if the property cannot be calculated as an integer
- Return type
Tuple[int, bool]
- Returns
value parsed to integer
ok: will be set to
True
if conversion was successful
See also
See also
See also
See also
See also
See also
- 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) –
QgsExpressionContext
to evaluate the property for.defaultString (str = '') – default string to return if the property cannot be calculated as a string
- Return type
Tuple[str, bool]
- Returns
value parsed to string
ok: will be set to
True
if conversion was successful
See also
See also
See also
See also
See also
See also
- 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
New in version 3.20.
- Parameters
variantMap (Dict[str) –
- Return type
Dict[str, QgsProperty]