Class: QgsWidgetWrapper

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.

Note

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

The following methods must be implemented: createWidget(), setFeature(), valid()

Class Hierarchy

Inheritance diagram of qgis.gui.QgsWidgetWrapper

Base classes

QObject

Subclasses

QgsActionWidgetWrapper

Wraps a button widget to launch a layer action.

QgsSearchWidgetWrapper

Shows a search widget on a filter form.

QgsEditorWidgetWrapper

Manages an editor widget.

QgsHtmlWidgetWrapper

Wraps a QQuickWidget to display HTML code.

QgsQmlWidgetWrapper

Wraps a QQuickWidget to display QML code.

QgsRelationWidgetWrapper

A widget wrapper for relation widgets.

QgsSpacerWidgetWrapper

Wraps a spacer widget.

QgsTextWidgetWrapper

Wraps a label widget to display text.

Enums

Property

Data defined properties for different editor widgets.

Abstract Methods

createWidget

This method should create a new widget with the provided parent.

setFeature

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

valid

Returns True if the widget has been properly initialized.

Methods

config

Use this inside your overridden classes to access the configuration.

context

Returns information about the context in which this widget is shown

dataDefinedProperties

Returns a reference to the editor widget's property collection, used for data defined overrides.

layer

Returns the vector layer associated with the widget.

notifyAboutToSave

Notify this widget, that the containing form is about to save and that any pending changes should be pushed to the edit buffer or they might be lost.

propertyDefinitions

Returns the editor widget property definitions.

setConfig

Will set the config of this wrapper to the specified config.

setContext

Set the context in which this widget is shown

setDataDefinedProperties

Sets the editor widget's property collection, used for data defined overrides.

widget

Access the widget managed by this wrapper

Virtual Methods

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

initWidget

This method should initialize the editor widget with runtime data.

setEnabled

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

Static Methods

fromWidget

Will return a wrapper for a given widget

Signals

contextChanged

Signal when QgsAttributeEditorContext mContext changed

class qgis.gui.QgsWidgetWrapper[source]

Bases: QObject

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

Create a new widget wrapper

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

  • 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 Property(*values)

Bases: IntEnum

Data defined properties for different editor widgets.

  • RootPath: Root path for external resource

  • DocumentViewerContent: Document type for external resource

  • StorageUrl: Storage URL for external resource

config(self, key: str | None, defaultVal: Any = None) Any[source]

Use this inside your overridden classes to access the configuration.

Parameters:
  • key (Optional[str]) – The configuration option you want to load

  • defaultVal (Any = None) – Default value

Return type:

Any

Returns:

the value assigned to this configuration option

config(self) Dict[str, Any][source]

Returns the whole config

Return type:

Dict[str, Any]

context(self) QgsAttributeEditorContext

Returns information about the context in which this widget is shown

Return type:

QgsAttributeEditorContext

signal contextChanged[source]

Signal when QgsAttributeEditorContext mContext changed

Added in version 3.4.

abstract createWidget(self, parent: QWidget | None) QWidget | None[source]

This method should create a new widget with the provided parent. 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().

Parameters:

parent (Optional[QWidget]) – You should set this parent on the created widget.

Return type:

Optional[QWidget]

Returns:

A new widget

dataDefinedProperties(self) QgsPropertyCollection

Returns a reference to the editor widget’s property collection, used for data defined overrides.

See also

Property

Return type:

QgsPropertyCollection

static fromWidget(widget: QWidget | None) QgsWidgetWrapper | 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[QgsWidgetWrapper]

Returns:

The wrapper for the widget or None

virtual initWidget(self, editor: QWidget | None)[source]

This method should initialize the editor widget with runtime data. Fill your comboboxes here.

Parameters:

editor (Optional[QWidget]) – The widget which will represent this attribute editor in a form.

layer(self) QgsVectorLayer | None[source]

Returns the vector layer associated with the widget.

Return type:

Optional[QgsVectorLayer]

notifyAboutToSave(self)[source]

Notify this widget, that the containing form is about to save and that any pending changes should be pushed to the edit buffer or they might be lost.

Added in version 3.2.

propertyDefinitions() Dict[int, QgsPropertyDefinition]

Returns the editor widget property definitions.

Return type:

Dict[int, QgsPropertyDefinition]

setConfig(self, config: Dict[str, Any])[source]

Will set the config of this wrapper to the specified config.

Parameters:

config (Dict[str, Any]) – The config for this wrapper

setContext(self, context: QgsAttributeEditorContext)[source]

Set the context in which this widget is shown

Parameters:

context (QgsAttributeEditorContext) – context information

setDataDefinedProperties(self, collection: QgsPropertyCollection)[source]

Sets the editor widget’s property collection, used for data defined overrides.

Parameters:

collection (QgsPropertyCollection) – property collection. Existing properties will be replaced.

See also

Property

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

Is used to enable or disable the edit functionality of the managed widget. By default this will not change the enabled state of the widget

Parameters:

enabled (bool) – Enable or Disable?

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

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

Parameters:

feature (QgsFeature) – The new feature

abstract valid(self) bool[source]

Returns True if the widget has been properly initialized. 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.

Return type:

bool

Returns:

Validity status of this widget.

widget(self) QWidget | None[source]

Access the widget managed by this wrapper

Return type:

Optional[QWidget]

Returns:

The widget