Data widget

dawiq.datawidget provides DataWidget to represent the data structure established by the dataclass.

class dawiq.datawidget.DataWidget(*args: Any, **kwargs: Any)[source]

Bases: QGroupBox

Group box for structured data.

This group box contains the field widgets as subwidgets. dataValue() returns the dictionary of field values and setDataValue() sets the values to the subwidgets.

Whenever the data value changes, dataValueChanged signal is emitted. When any field is edited by the user, dataEdited signal is emitted.

This widget is also used to represent the nested dataclass field, therefore it follows FieldWidgetProtocol.

Notes

This class can be constructed from dataclass2Widget(), but the widget does not store the dataclass type. To associate the data widget to the dataclass use DataclassDelegate.

fieldValueChanged

alias of dict

orientation() dawiq.qt_compat.QtCore.Qt.Orientation[source]

Orientation to stack the subwidgets.

count() int[source]

Number of subwidgets.

widget(index: int) Optional[FieldWidgetProtocol][source]

Returns the subwidget at the given index, or None for invalid index.

insertWidget(index: int, widget: FieldWidgetProtocol, stretch: int = 0, alignment: dawiq.qt_compat.QtCore.Qt.AlignmentFlag = dawiq.qt_compat.QtCore.Qt.AlignmentFlag)[source]

Insert the widget to layout and connect the signals.

addWidget(widget: FieldWidgetProtocol, stretch: int = 0, alignment: dawiq.qt_compat.QtCore.Qt.AlignmentFlag = dawiq.qt_compat.QtCore.Qt.AlignmentFlag)[source]

Add the widget to layout and connect the signals.

removeWidget(widget: FieldWidgetProtocol)[source]

Remove the widget from layout and disconnect the signals.

setRequired(required: bool)[source]

Recursively set required to all subwidgets.

dawiq.datawidget.type2Widget(t: Any) FieldWidgetProtocol[source]

Construct the widget for given type annotation t.

The following types are supported. Dataclass type is not converted here but by dataclass2Widget().

For Tuple, its length must be finite (no Ellipsis in args) and item types must be the supported type.

dawiq.datawidget.dataclass2Widget(dcls: ~typing.Type[~dawiq.typing.DataclassProtocol], field_converter: ~typing.Callable[[~typing.Any], ~dawiq.typing.FieldWidgetProtocol] = <function type2Widget>, orientation: dawiq.qt_compat.QtCore.Qt.Orientation = dawiq.qt_compat.QtCore.Qt.Orientation.Vertical, globalns: ~typing.Optional[~typing.Dict] = None, localns: ~typing.Optional[~typing.Dict] = None, include_extras: bool = False) DataWidget[source]

Construct DataWidget from dcls.

Each field of dcls is converted to field widget by type2Widget() with the type hint of the field. If the field has Qt_typehint metadata, its value is used instead of the type hint.

If the field type is dataclass, construction is recursively done with same parameters.

Parameters
dcls

Dataclass type which will be converted to widget.

field_converter

Callable to convert non-dataclass fields to widgets. Default is type2Widget.

orientation

Argument for DataWidget.

globalns, localns, include_extras

Arguments for get_type_hints() to resolve the forward-referenced type annotations.