QgsValueRelationWidgetWrapper class
Wraps a value relation widget.
Contents
This widget will offer a combobox with values from another layer referenced by a foreign key (a constraint may be set but is not required on data level). This is useful for having value lists on a separate layer containing codes and their translation to human readable names.
Options:
- Layer The id of the referenced layer.
- Key The key field on the referenced layer (code).
- Value The value field on the referenced layer (human readable name).
- AllowMulti If set to True, will allow multiple selections. This requires the data type to be a string. This does NOT work with normalized database structures.
- AllowNull Will offer NULL as a possible value.
- FilterExpression If not empty, will be used as expression. Only if this evaluates to True, the value will be shown.
- OrderByValue Will order by value instead of key.
Base classes
- class QgsEditorWidgetWrapper
- Manages an editor widget Widget and wrapper share the same parent.
Constructors, destructors, conversion operators
- QgsValueRelationWidgetWrapper(QgsVectorLayer* layer, int fieldIdx, QWidget* editor = nullptr, QWidget* parent = nullptr) explicit
- Constructor for QgsValueRelationWidgetWrapper.
Public functions
- void setEnabled(bool enabled) override
- Is used to enable or disable the edit functionality of the managed widget.
- void showIndeterminateState() override
- Sets the widget to display in an indeterminate "mixed value" state.
- auto value() const -> QVariant override
- Will be used to access the widget's value.
Public slots
- void setFeature(const QgsFeature& feature) override
- Will be called when the feature changes.
- void widgetValueChanged(const QString& attribute, const QVariant& newValue, bool attributeChanged)
- Will be called when a value in the current edited form or table row changes.
Protected functions
- auto createWidget(QWidget* parent) -> QWidget* override
- This method should create a new widget with the provided parent.
- void initWidget(QWidget* editor) override
- This method should initialize the editor widget with runtime data.
- auto valid() const -> bool override
- Returns true if the widget has been properly initialized.
Function documentation
QgsValueRelationWidgetWrapper:: QgsValueRelationWidgetWrapper(QgsVectorLayer* layer,
int fieldIdx,
QWidget* editor = nullptr,
QWidget* parent = nullptr) explicit
Constructor for QgsValueRelationWidgetWrapper.
The layer and fieldIdx arguments specify the vector layer field associated with the wrapper.
The editor argument indicates the editor widget to use with the wrapper. This can be a nullptr if a new widget should be autogenerated.
A parent widget for this widget wrapper and the created widget can also be specified.
void QgsValueRelationWidgetWrapper:: 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 not change the enabled state of the widget
void QgsValueRelationWidgetWrapper:: showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
QVariant QgsValueRelationWidgetWrapper:: value() const override
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 QgsValueRelationWidgetWrapper:: 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() and updates the widget cache if the filter expression context contains values from the current feature
void QgsValueRelationWidgetWrapper:: widgetValueChanged(const QString& attribute,
const QVariant& newValue,
bool attributeChanged) public slot
Will be called when a value in the current edited form or table row changes.
| Parameters | |
|---|---|
| attribute | The name of the attribute that changed. |
| newValue | The new value of the attribute. |
| attributeChanged | If true, it corresponds to an actual change of the feature attribute |
Update widget cache if the value is used in the filter expression and stores current field values to be used in expression form scope context
QWidget* QgsValueRelationWidgetWrapper:: createWidget(QWidget* parent) override protected
This method should create a new widget with the provided parent.
| Parameters | |
|---|---|
| parent | You should set this parent on the created widget. |
| Returns | A new widget |
This will only be called if the form did not already provide a widget, so it is not guaranteed to be called! You should not do initialization stuff, which also has to be done for custom editor widgets inside this method. Things like filling comboboxes and assigning other data which will also be used to make widgets on forms created in the QtDesigner usable should be assigned in initWidget().
void QgsValueRelationWidgetWrapper:: initWidget(QWidget* editor) override protected
This method should initialize the editor widget with runtime data.
| Parameters | |
|---|---|
| editor | The widget which will represent this attribute editor in a form. |
Fill your comboboxes here.
bool QgsValueRelationWidgetWrapper:: valid() const override protected
Returns true if the widget has been properly initialized.
| Returns | Validity status of this widget. |
|---|
This acts as hint for the calling party if this wrapper can be used after initializing it. If it cannot be used this is a hint to the caller that he may try to find another suitable widget type instead.