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:
QGroupBoxGroup box for structured data.
This group box contains the field widgets as subwidgets.
dataValue()returns the dictionary of field values andsetDataValue()sets the values to the subwidgets.Whenever the data value changes,
dataValueChangedsignal is emitted. When any field is edited by the user,dataEditedsignal 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 useDataclassDelegate.- widget(index: int) FieldWidgetProtocol | None[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.
- 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().bool->BoolCheckBoxOptional[bool]->BoolCheckBoxwith tristateintorOptional[int]->IntLineEditfloatorOptional[float]->FloatLineEditstrorOptional[str]->StrLineEditTuple->TupleGroupBoxwith nested field widgets
For
Tuple, its length must be finite (noEllipsisin args) and item types must be the supported type.
- dawiq.datawidget.dataclass2Widget(dcls: ~typing.Type[DataclassInstance], 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.Dict | None = None, localns: ~typing.Dict | None = None, include_extras: bool = False) DataWidget[source]
Construct
DataWidgetfrom dcls.Each field of dcls is converted to field widget by
type2Widget()with the type hint of the field. If the field hasQt_typehintmetadata, 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.