QgsEditorWidgetWrapper class

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.

Base classes

class QgsWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.

Derived classes

class QgsBinaryWidgetWrapper
Widget wrapper for binary (BLOB) fields.
class QgsCheckboxWidgetWrapper
Wraps a checkbox widget.
class QgsClassificationWidgetWrapper
class QgsColorWidgetWrapper
Wraps a color widget.
class QgsDateTimeEditWrapper
Wraps a date time widget.
class QgsEnumerationWidgetWrapper
class QgsExternalResourceWidgetWrapper
Wraps a file name widget.
class QgsHiddenWidgetWrapper
Wraps a hidden widget.
class QgsKeyValueWidgetWrapper
Wraps a key/value widget.
class QgsListWidgetWrapper
Wraps a list widget.
class QgsRangeWidgetWrapper
Wraps a range widget.
class QgsRelationReferenceWidgetWrapper
Wraps a relation reference widget.
class QgsTextEditWrapper
Wraps a text widget.
class QgsUniqueValuesWidgetWrapper
Wraps a unique value widget.
class QgsUuidWidgetWrapper
Wraps a uuid widget.
class QgsValueMapWidgetWrapper
Wraps a value map widget.
class QgsValueRelationWidgetWrapper
Wraps a value relation widget.

Public types

enum ConstraintResult { ConstraintResultPass = 0, ConstraintResultFailHard, ConstraintResultFailSoft }
Result of constraint checks.

Public static functions

static auto fromWidget(QWidget* widget) -> QgsEditorWidgetWrapper*
Will return a wrapper for a given widget.
static auto isInTable(const QWidget* parent) -> bool
Check if the given widget or one of its parent is a QTableView.

Constructors, destructors, conversion operators

QgsEditorWidgetWrapper(QgsVectorLayer* vl, int fieldIdx, QWidget* editor = nullptr, QWidget* parent = nullptr) explicit
Create a new widget wrapper.

Public functions

auto constraintFailureReason() const -> QString
Returns the reason why a constraint check has failed (or an empty string if constraint check was successful).
auto constraintResult() const -> ConstraintResult
Returns the constraint result, which is the current result of the constraint on the widget influencing its visualization.
auto constraintResultVisible() const -> bool
Returns whether the constraint result is visible.
auto defaultValue() const -> QVariant
Access the default value of the field.
auto field() const -> QgsField
Access the field.
auto fieldIdx() const -> int
Access the field index.
auto isBlockingCommit() const -> bool
Returns true if the widget is preventing the feature from being committed.
auto isValidConstraint() const -> bool
Gets the current constraint status.
void setConstraintResultVisible(bool constraintResultVisible)
Sets whether the constraint result is visible.
void setEnabled(bool enabled) override
Is used to enable or disable the edit functionality of the managed widget.
void setHint(const QString& hintText) virtual
Add a hint text on the widget.
void showIndeterminateState() virtual
Sets the widget to display in an indeterminate "mixed value" state.
void updateConstraint(const QgsFeature& featureContext, QgsFieldConstraints::ConstraintOrigin constraintOrigin = QgsFieldConstraints::ConstraintOriginNotSet)
Update constraint.
void updateConstraint(const QgsVectorLayer* layer, int index, const QgsFeature& feature, QgsFieldConstraints::ConstraintOrigin constraintOrigin = QgsFieldConstraints::ConstraintOriginNotSet)
Update constraint on a feature coming from a specific layer.
auto value() const -> QVariant pure virtual
Will be used to access the widget's value.

Signals

void constraintResultVisibleChanged(bool visible)
Emit this signal when the constraint result visibility changed.
void constraintStatusChanged(const QString& constraint, const QString& desc, const QString& err, QgsEditorWidgetWrapper::ConstraintResult status)
Emit this signal when the constraint status changed.
void valueChanged(const QVariant& value)
Emit this signal, whenever the value changed.

Public slots

void emitValueChanged()
Will call the value() method to determine the emitted value.
void setFeature(const QgsFeature& feature) override
Will be called when the feature changes.
void setValue(const QVariant& value) pure virtual
Is called, when the value of the widget needs to be changed.

Protected functions

auto formFeature() const -> QgsFeature
The feature currently being edited, in its current state.
void setFormFeature(const QgsFeature& feature)
Set the feature currently being edited to feature.
auto setFormFeatureAttribute(const QString& attributeName, const QVariant& attributeValue) -> bool
Update the feature currently being edited by changing its attribute attributeName to attributeValue.
void updateConstraintWidgetStatus() virtual
This should update the widget with a visual cue if a constraint status changed.

Enum documentation

enum QgsEditorWidgetWrapper::ConstraintResult

Result of constraint checks.

Enumerators
ConstraintResultPass

Widget passed constraints successfully.

ConstraintResultFailHard

Widget failed at least one hard (enforced) constraint.

ConstraintResultFailSoft

Widget failed at least one soft (non-enforced) constraint.

Function documentation

static QgsEditorWidgetWrapper* QgsEditorWidgetWrapper::fromWidget(QWidget* widget)

Will return a wrapper for a given widget.

Parameters
widget The widget which was created by a wrapper
Returns The wrapper for the widget or NULL

static bool QgsEditorWidgetWrapper::isInTable(const QWidget* parent)

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

Parameters
parent the widget to check
Returns true if yes

QgsEditorWidgetWrapper::QgsEditorWidgetWrapper(QgsVectorLayer* vl, int fieldIdx, QWidget* editor = nullptr, QWidget* parent = nullptr) explicit

Create a new widget wrapper.

Parameters
vl The layer on which the field is
fieldIdx The field which will be controlled
editor An editor widget. Can be NULL if one should be autogenerated.
parent A parent widget for this widget wrapper and the created widget.

QString QgsEditorWidgetWrapper::constraintFailureReason() const

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

ConstraintResult QgsEditorWidgetWrapper::constraintResult() const

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

bool QgsEditorWidgetWrapper::constraintResultVisible() const

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.

QVariant QgsEditorWidgetWrapper::defaultValue() const

Access the default value of the field.

Returns the default value of the field

QgsField QgsEditorWidgetWrapper::field() const

Access the field.

Returns The field you are working on

int QgsEditorWidgetWrapper::fieldIdx() const

Access the field index.

Returns The index of the field you are working on

bool QgsEditorWidgetWrapper::isBlockingCommit() const

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.

bool QgsEditorWidgetWrapper::isValidConstraint() const

Gets the current constraint status.

Returns true if the constraint is valid or if there's no constraint, false otherwise

void QgsEditorWidgetWrapper::setConstraintResultVisible(bool constraintResultVisible)

Sets whether the constraint result is visible.

Parameters
constraintResultVisible if constraintResult should be displayed (mostly editable status)

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

void QgsEditorWidgetWrapper::setEnabled(bool enabled) override

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

Parameters
enabled Enable or Disable?

By default this will enable or disable the whole widget

void QgsEditorWidgetWrapper::setHint(const QString& hintText) virtual

Add a hint text on the widget.

Parameters
hintText The hint text to display

void QgsEditorWidgetWrapper::showIndeterminateState() virtual

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

void QgsEditorWidgetWrapper::updateConstraint(const QgsFeature& featureContext, QgsFieldConstraints::ConstraintOrigin constraintOrigin = QgsFieldConstraints::ConstraintOriginNotSet)

Update constraint.

Parameters
featureContext the feature to use to evaluate the constraint
constraintOrigin optional origin for constraints to check. This can be used to limit the constraints tested to only provider or layer based constraints.

void QgsEditorWidgetWrapper::updateConstraint(const QgsVectorLayer* layer, int index, const QgsFeature& feature, QgsFieldConstraints::ConstraintOrigin constraintOrigin = QgsFieldConstraints::ConstraintOriginNotSet)

Update constraint on a feature coming from a specific layer.

Parameters
layer The vector layer where the feature is defined
index The index of the field to check
feature The feature to use to evaluate the constraint
constraintOrigin Optional origin for constraints to check. This can be used to limit the constraints tested to only provider or layer based constraints.

QVariant QgsEditorWidgetWrapper::value() const pure virtual

Will be used to access the widget's value.

Returns The current value the widget represents

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.

void QgsEditorWidgetWrapper::constraintStatusChanged(const QString& constraint, const QString& desc, const QString& err, QgsEditorWidgetWrapper::ConstraintResult status) signal

Emit this signal when the constraint status changed.

Parameters
constraint represented as a string
desc is the constraint description
err the error represented as a string. Empty if none.
status

constraintStatusChanged

void QgsEditorWidgetWrapper::valueChanged(const QVariant& value) signal

Emit this signal, whenever the value changed.

Parameters
value The new value

void QgsEditorWidgetWrapper::setFeature(const QgsFeature& feature) override public slot

Will be called when the feature changes.

Parameters
feature The new feature

Is forwarded to the slot setValue()

void QgsEditorWidgetWrapper::setValue(const QVariant& value) pure virtual public slot

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

Parameters
value The new value of the attribute

Update the widget representation to reflect the new value.

QgsFeature QgsEditorWidgetWrapper::formFeature() const protected

The feature currently being edited, in its current state.

Returns the feature currently being edited, in its current state

void QgsEditorWidgetWrapper::setFormFeature(const QgsFeature& feature) protected

Set the feature currently being edited to feature.

bool QgsEditorWidgetWrapper::setFormFeatureAttribute(const QString& attributeName, const QVariant& attributeValue) protected

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

Returns bool true on success

void QgsEditorWidgetWrapper::updateConstraintWidgetStatus() virtual protected

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.