Class: QgsEditorWidgetWrapper

Manages an editor widget.

Widget and wrapper share the same parent

A wrapper controls one attribute editor widget and is able to create a default widget or use a pre-existent widget. It is able to set the widget to the value implied by a field of a vector layer, or return the value it currently holds. Every time it is changed it has to emit a valueChanged signal. If it fails to do so, there is no guarantee that the changed status of the widget will be saved.

It can also handle additional fields of a vector layer and would set the widget for their corresponding values and emit valuesChanged signal.

Note

This is an abstract class, with methods which must be implemented by a subclass.

The following methods must be implemented: QgsWidgetWrapper.createWidget(), QgsWidgetWrapper.valid(), value()

Class Hierarchy

Inheritance diagram of qgis.gui.QgsEditorWidgetWrapper

Base classes

QgsWidgetWrapper

Manages an editor widget.

QObject

Subclasses

QgsRelationReferenceWidgetWrapper

Wraps a relation reference widget.

Abstract Methods

value

Will be used to access the widget's value.

Methods

constraintFailureReason

Returns the reason why a constraint check has failed (or an empty string if constraint check was successful).

constraintResult

Returns the constraint result, which is the current result of the constraint on the widget influencing its visualization.

constraintResultVisible

Returns whether the constraint result is visible.

defaultValue

Access the default value of the field.

emitValueChanged

Will call the value() method to determine the emitted value

field

Access the field.

fieldIdx

Access the field index.

formFeature

The feature currently being edited, in its current state

isBlockingCommit

Returns True if the widget is preventing the feature from being committed.

isValidConstraint

Gets the current constraint status.

setConstraintResultVisible

Sets whether the constraint result is visible.

setFormFeature

Set the feature currently being edited to feature

setFormFeatureAttribute

Update the feature currently being edited by changing its attribute attributeName to attributeValue

setValues

Is called when the value of the widget or additional field values needs to be changed.

updateConstraint

Update constraint.

Virtual Methods

In PyQGIS, only methods marked as virtual can be safely overridden in a Python subclass of QgsEditorWidgetWrapper. See the FAQ for more details.

additionalFieldValues

Will be used to access the widget's values for potential additional fields handled by the widget

additionalFields

Returns the list of additional fields which the editor handles

parentFormValueChanged

Is called in embedded form widgets when an attribute value in the parent form has changed.

setEnabled

Is used to enable or disable the edit functionality of the managed widget.

setFeature

Will be called when the feature changes

setHint

Add a hint text on the widget

setValue

Is called when the value of the widget needs to be changed.

showIndeterminateState

Sets the widget to display in an indeterminate "mixed value" state.

updateConstraintWidgetStatus

This should update the widget with a visual cue if a constraint status changed.

Static Methods

fromWidget

Will return a wrapper for a given widget

isInTable

Check if the given widget or one of its parent is a QTableView.

Signals

constraintResultVisibleChanged

Emit this signal when the constraint result visibility changed.

constraintStatusChanged

Emit this signal when the constraint status changed.

valueChanged

Emit this signal, whenever the value changed.

valuesChanged

Emit this signal, whenever the value changed.

Attributes

ConstraintResultFailHard

ConstraintResultFailSoft

ConstraintResultPass

class qgis.gui.QgsEditorWidgetWrapper[source]

Bases: QgsWidgetWrapper

__init__(vl: QgsVectorLayer | None, fieldIdx: int, editor: QWidget | None = None, parent: QWidget | None = None)

Create a new widget wrapper

Parameters:
  • vl (Optional[QgsVectorLayer]) – The layer on which the field is

  • fieldIdx (int) – The field which will be controlled

  • editor (Optional[QWidget] = None) – An editor widget. Can be None if one should be autogenerated.

  • parent (Optional[QWidget] = None) – A parent widget for this widget wrapper and the created widget.

class ConstraintResult

Bases: int

ConstraintResultFailHard = 1
ConstraintResultFailSoft = 2
ConstraintResultPass = 0
virtual additionalFieldValues(self) List[Any][source]

Will be used to access the widget’s values for potential additional fields handled by the widget

Return type:

List[Any]

Returns:

A map of additional field names with their corresponding values

Added in version 3.10.

virtual additionalFields(self) List[str][source]

Returns the list of additional fields which the editor handles

Added in version 3.10.

Return type:

List[str]

constraintFailureReason(self) str[source]

Returns the reason why a constraint check has failed (or an empty string if constraint check was successful).

Return type:

str

constraintResult(self) QgsEditorWidgetWrapper.ConstraintResult[source]

Returns the constraint result, which is the current result of the constraint on the widget influencing its visualization.

Return type:

QgsEditorWidgetWrapper.ConstraintResult

constraintResultVisible(self) bool[source]

Returns whether the constraint result is visible.

Returns True if the constraint result will be visualized on the widget (with color). This will be disabled when the form is not editable.

Return type:

bool

signal constraintResultVisibleChanged(visible: bool)[source]

Emit this signal when the constraint result visibility changed.

Parameters:

visible (bool)

signal constraintStatusChanged(constraint: str, desc: str, err: str, status: QgsEditorWidgetWrapper.ConstraintResult)[source]

Emit this signal when the constraint status changed. constraintStatusChanged

Parameters:
  • constraint (str) – represented as a string

  • desc (str) – is the constraint description

  • err (str) – the error represented as a string. Empty if none.

  • status (QgsEditorWidgetWrapper.ConstraintResult)

defaultValue(self) Any[source]

Access the default value of the field.

Return type:

Any

Returns:

the default value of the field

See also

layer()

emitValueChanged(self)[source]

Will call the value() method to determine the emitted value

field(self) QgsField[source]

Access the field.

Return type:

QgsField

Returns:

The field you are working on

See also

layer()

fieldIdx(self) int[source]

Access the field index.

Return type:

int

Returns:

The index of the field you are working on

See also

layer()

formFeature(self) QgsFeature[source]

The feature currently being edited, in its current state

Return type:

QgsFeature

Returns:

the feature currently being edited, in its current state

Added in version 3.2.

static fromWidget(widget: QWidget | None) QgsEditorWidgetWrapper | None[source]

Will return a wrapper for a given widget

Parameters:

widget (Optional[QWidget]) – The widget which was created by a wrapper

Return type:

Optional[QgsEditorWidgetWrapper]

Returns:

The wrapper for the widget or None

isBlockingCommit(self) bool[source]

Returns True if the widget is preventing the feature from being committed. This may be True as a result of attribute values failing enforced field constraints.

Return type:

bool

static isInTable(parent: QWidget | None) bool[source]

Check if the given widget or one of its parent is a QTableView.

Parameters:

parent (Optional[QWidget]) – the widget to check

Return type:

bool

Returns:

True if yes

isValidConstraint(self) bool[source]

Gets the current constraint status.

Return type:

bool

Returns:

True if the constraint is valid or if there’s no constraint, False otherwise

virtual parentFormValueChanged(self, attribute: str | None, value: Any)[source]

Is called in embedded form widgets when an attribute value in the parent form has changed.

The default implementations does nothing. Subclasses should reimplement this method to notify the form widgets that something has changed in case they have filter expressions that depend on the parent form scope.

Added in version 3.14.

Parameters:
  • attribute (Optional[str])

  • value (Any)

setConstraintResultVisible(self, constraintResultVisible: bool)[source]

Sets whether the constraint result is visible.

Controls if the constraint result should be visualized on the widget (with color). This will be disabled when the form is not editable.

Parameters:

constraintResultVisible (bool) – if constraintResult should be displayed (mostly editable status)

virtual setEnabled(self, enabled: bool)[source]

Is used to enable or disable the edit functionality of the managed widget. By default this will enable or disable the whole widget

Parameters:

enabled (bool) – Enable or Disable?

virtual setFeature(self, feature: QgsFeature)[source]

Will be called when the feature changes

Is forwarded to the slot setValues()

Parameters:

feature (QgsFeature) – The new feature

setFormFeature(self, feature: QgsFeature)[source]

Set the feature currently being edited to feature

Added in version 3.2.

Parameters:

feature (QgsFeature)

setFormFeatureAttribute(self, attributeName: str | None, attributeValue: Any) bool[source]

Update the feature currently being edited by changing its attribute attributeName to attributeValue

Return type:

bool

Returns:

bool True on success

Added in version 3.2.

Parameters:
  • attributeName (Optional[str])

  • attributeValue (Any)

virtual setHint(self, hintText: str | None)[source]

Add a hint text on the widget

Parameters:

hintText (Optional[str]) – The hint text to display

virtual setValue(self, value: Any)[source]

Is called when the value of the widget needs to be changed. Updates the widget representation to reflect the new value.

Parameters:

value (Any) – The new value of the attribute

Deprecated since version 3.10.

setValues(self, value: Any, additionalValues: Iterable[Any])[source]

Is called when the value of the widget or additional field values needs to be changed. Updates the widget representation to reflect the new values.

Added in version 3.10.

Parameters:
  • value (Any)

  • additionalValues (Iterable[Any])

virtual showIndeterminateState(self)[source]

Sets the widget to display in an indeterminate “mixed value” state.

updateConstraint(self, featureContext: QgsFeature, constraintOrigin: QgsFieldConstraints.ConstraintOrigin = QgsFieldConstraints.ConstraintOriginNotSet)[source]

Update constraint.

Parameters:
  • featureContext (QgsFeature) – the feature to use to evaluate the constraint

  • constraintOrigin (QgsFieldConstraints.ConstraintOrigin = QgsFieldConstraints.ConstraintOriginNotSet) – optional origin for constraints to check. This can be used to limit the constraints tested to only provider or layer based constraints.

updateConstraint(self, layer: QgsVectorLayer | None, index: int, feature: QgsFeature, constraintOrigin: QgsFieldConstraints.ConstraintOrigin = QgsFieldConstraints.ConstraintOriginNotSet)[source]

Update constraint on a feature coming from a specific layer.

Parameters:
  • layer (Optional[QgsVectorLayer]) – The vector layer where the feature is defined

  • index (int) – The index of the field to check

  • feature (QgsFeature) – The feature to use to evaluate the constraint

  • constraintOrigin (QgsFieldConstraints.ConstraintOrigin = QgsFieldConstraints.ConstraintOriginNotSet) – Optional origin for constraints to check. This can be used to limit the constraints tested to only provider or layer based constraints.

updateConstraint(self, constraintResult: QgsEditorWidgetWrapper.ConstraintResult, constraintFailureReason: str | None)[source]

Update constraint manually by providing the constraint result value and failure reason(s).

Parameters:
  • constraintResult (QgsEditorWidgetWrapper.ConstraintResult) – the constraint result value

  • constraintFailureReason (Optional[str]) – the constraint failure reason(s) (blank is the result passes)

Added in version 3.36.

virtual updateConstraintWidgetStatus(self)[source]

This should update the widget with a visual cue if a constraint status changed.

By default a stylesheet will be applied on the widget that changes the background color to red.

This can be overwritten in subclasses to allow individual widgets to change the visual cue.

abstract value(self) Any[source]

Will be used to access the widget’s value. Read the value from the widget and return it properly formatted to be saved in the attribute.

If an invalid variant is returned this will be interpreted as no change. Be sure to return a NULL QVariant if it should be set to NULL.

Return type:

Any

Returns:

The current value the widget represents

signal valueChanged(value: object)[source]

Emit this signal, whenever the value changed.

Parameters:

value (object) – The new value

Deprecated since version 3.10: Use valuesChanged signal instead.

signal valuesChanged(value: object, additionalFieldValues: List[object] = [])[source]

Emit this signal, whenever the value changed. It will also return the values for the additional fields handled by the widget

Parameters:
  • value (object) – The new value

  • additionalFieldValues (List[object] = []) – A map of additional field names with their corresponding values

Added in version 3.10.