Package: dataclass2PySide6
Python package to dynamically create PySide6 widgets from dataclass:warning: This package no longer actively maintained. UseDaWiQinstead.Introductiondataclass2PySide6 is a package which provides widgets to representdataclassinstance asPySide6GUI.For example, here is a simple dataclass:fromdataclassesimportdataclassfromtypingimportTuple@dataclassclassDataClass:a:intb:Tuple[float,bool]=(4.2,True)Dataclass widget can be dynamically created as follows.fromPySide6.QtWidgetsimportQApplicationimportsysfromdataclass2PySide6importDataclassWidgetapp=QApplication(sys.argv)widget=DataclassWidget.fromDataclass(DataClass)widget.show()app.exec()app.quit()InstallationBefore you install, be careful for other Qt-dependent packages installed in your environment.
For example, non-headlessOpenCV-Pythonmodule modifies the Qt dependency thus making PySide6 unavailable.dataclass2PySide6can be installed usingpip.$ pip install dataclass2PySide6How to useDataclassWidgetis a widget for single dataclass type.
For multiple dataclass types in one widget,StackedDataclassWidgetandTabDataclassWidgetare provided.Single dataclassDataclassWidgetis the core object ofdataclass2PySide6. It contains subwidgets which represent each fields of the dataclass.User may subclassDataclassWidgetto define own datclass widget. Refer to the docstring for detailed description.Creating widgetTo constructDataclassWidget, pass dataclass type toDataclassWidget.fromDataclass()method.To construct suitable widget for each field,DataclassWidgetsearches forQt_typehintmetadata.
Its value must be the type annotation, not necessarily identical totypeattribute of the field. SeeDataclass examplefor the use case ofQt_typehint.IfQt_typehintdoes not exist, it usestypeattribute of the field as a fallback.
Currently supported types are:Enum: converted to combo boxbool: converted to check box (UnionwithNoneallowed)intandfloat: converted to line edit with validator (UnionwithNoneallowed)str: converted to line editTuple: converted to group box if element types are supportedGetting data valueWhen data from any subwidget change,DataclassWidget.dataValueChangedsignal emits the new dataclass instance with current value.
For check box or combo box, this is when the current selection changes. For line edit, this is when editing is finished.DataclassWidget.dataValue()method returns the new dataclass instance with current data value.
If fields of the dataclass which constructed the widget definedfromQt_convertermetadata, they are used to preprocess the data before dataclass construction.
Its value must be a unary function which converts the widget data to field value.SeeDataclass examplefor the use case offromQt_converter.Setting data valueDataclassWidget.setDataValue()method updates the subwidget data with new dataclass instance.
If fields of the dataclass which constructed the widget definedtoQt_convertermetadata,
they are used to convert the value to the form which is updatable to the widget.
Its value must be a unary function which converts the field value to widget data.SeeDataclass examplefor the use case oftoQt_converter.Multiple dataclassesStackedDataclassWidgetandTabDataclassWidgetprovide multipleDataclassWidgetin one widget.Both classes provideaddDataclass()method to add new widget for dataclass, andindexOfDataclass()method to search widget for dataclass.
When current widget's value is changed,dataValuechangedsignal emits the new dataclass instance with current value.Note that changing the current index (or current widget) does not emitdataValuechangedsignal.
Listen to standardcurrentChangedsignal to detect it.Dataclass exampleThis is the example of dataclass which includes custom object.
Widget is constructed byTuple[int, int]type annotation to represent two parameters ofMyObj.
Conversion fromtupletoMyObjand vice versa are defined.fromdataclassesimportfieldfromtypingimportTupleclassMyObj:def__init__(self,x:int,y:int):self.x=xself.y=y@dataclassclassDataClass:my_obj:MyObj=field(metadata=dict(Qt_typehint=Tuple[int,int],fromQt_converter=lambdatup:MyObj(*tup),toQt_converter=lambdaobj:(obj.x,obj.y)))This is the widget constructed fromDataClass.