Package to construct PySide6 widgets from dataclass
Project description
Python package to dynamically create PySide6 widgets from dataclass
Introduction
dataclass2PySide6 is a package which provides widgets to represent dataclass
instance as PySide6
GUI.
For example, here is a simple dataclass:
from dataclasses import dataclass
from typing import Tuple
@dataclass
class DataClass:
a: int
b: Tuple[float, bool] = (4.2, True)
Dataclass widget can be dynamically created as follows.
from PySide6.QtWidgets import QApplication
import sys
from dataclass2PySide6 import DataclassWidget
app = QApplication(sys.argv)
widget = DataclassWidget.fromDataclass(DataClass)
widget.show()
app.exec()
app.quit()
Installation
Before you install, be careful for other Qt-dependent packages installed in your environment.
For example, non-headless OpenCV-Python
module modifies the Qt dependency thus making PySide6 unavailable.
dataclass2PySide6
can be installed using pip
.
$ pip install dataclass2PySide6
How to use
DataclassWidget
is a widget for single dataclass type.
For multiple dataclass types in one widget, StackedDataclassWidget
and TabDataclassWidget
are provided.
Single dataclass
DataclassWidget
is the core object of dataclass2PySide6
. It contains subwidgets which represent each fields of the dataclass.
User may subclass DataclassWidget
to define own datclass widget. Refer to the docstring for detailed description.
Creating widget
To construct DataclassWidget
, pass dataclass type to DataclassWidget.fromDataclass()
method.
To construct suitable widget for each field, DataclassWidget
searches for Qt_typehint
metadata.
Its value must be the type annotation, not necessarily identical to type
attribute of the field. See Dataclass example for the use case of Qt_typehint
.
If Qt_typehint
does not exist, it uses type
attribute of the field as a fallback.
Currently supported types are:
Enum
: converted to combo boxbool
: converted to check box (Union
withNone
allowed)int
andfloat
: converted to line edit with validator (Union
withNone
allowed)str
: converted to line editTuple
: converted to group box if element types are supported
Getting data value
When data from any subwidget change, DataclassWidget.dataValueChanged
signal 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 defined fromQt_converter
metadata, 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.
See Dataclass example for the use case of fromQt_converter
.
Setting data value
DataclassWidget.setDataValue()
method updates the subwidget data with new dataclass instance.
If fields of the dataclass which constructed the widget defined toQt_converter
metadata,
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.
See Dataclass example for the use case of toQt_converter
.
Multiple dataclasses
StackedDataclassWidget
and TabDataclassWidget
provide multiple DataclassWidget
in one widget.
Both classes provide addDataclass()
method to add new widget for dataclass, and indexOfDataclass()
method to search widget for dataclass.
When current widget's value is changed, dataValuechanged
signal emits the new dataclass instance with current value.
Dataclass example
This is the example of dataclass which includes custom object.
Widget is constructed by Tuple[int, int]
type annotation to represent two parameters of MyObj
.
Conversion from tuple
to MyObj
and vice versa are defined.
from dataclasses import field
from typing import Tuple
class MyObj:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
@dataclass
class DataClass:
my_obj: MyObj = field(
metadata=dict(
Qt_typehint=Tuple[int, int],
fromQt_converter=lambda tup: MyObj(*tup),
toQt_converter=lambda obj: (obj.x, obj.y)
)
)
This is the widget constructed from DataClass
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dataclass2PySide6-1.8.0.tar.gz
.
File metadata
- Download URL: dataclass2PySide6-1.8.0.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f42452c5b2e1ac8d2b9c88df97e32c6db9ca44443f3f2ef0a667c60fb3ac805d |
|
MD5 | 91a1eada71911b55cab339c1d019d4c6 |
|
BLAKE2b-256 | e3c60259ce3a03b04676ad8253485d689650a0b086ef0ad140fb9ca77d705865 |
File details
Details for the file dataclass2PySide6-1.8.0-py3-none-any.whl
.
File metadata
- Download URL: dataclass2PySide6-1.8.0-py3-none-any.whl
- Upload date:
- Size: 18.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b7bfb970433571fcced44363ffbf0d86793f01b00b7d4dcb16239ea46d531a0 |
|
MD5 | 5d5ca4b07be4131c7a9b00f193f94b9a |
|
BLAKE2b-256 | 540774fdb6860affefb11a102da420bc3d45545fa3fa2cf4875cdc6a3b05110f |