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 object 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.
For example,
from dataclasses import field
from typing import Union
@dataclass
class DataClass:
x: Union[int, float] = field(metadata=dict(Qt_typehint=float))
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 (UnionwithNoneallowed)intandfloat: converted to line edit with validator (UnionwithNoneallowed)str: converted to line editTuple: converted to group box if element types are supported
Getting data value
When data from any subwidget changes, 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.
Fields can define Qt_converter metadata to preprocess the data before dataclass construction.
Its value must be a unary function which takes the data from the field's widget.
For example,
@dataclass
class DataClass:
x: str = field(metadata=dict(Qt_converter=lambda s: s.upper()))
Setting data value
DataclassWidget.setDataValue() method updates the subwidget data with new dataclass instance.
Multiple dataclasses
StackedDataclassWidget and TabDataclassWidget provide multiple DataclassWidget in one widget.
Both classes provide addDataclass() method to add new widget for dataclass, and indexOf() method to search widget for dataclass.
When current widget's value is changed, dataValuechanged signal emits the new dataclass instance with current value.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dataclass2PySide6-1.7.0.tar.gz.
File metadata
- Download URL: dataclass2PySide6-1.7.0.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbe1fb1ad2b5d684dae1e3efc8515230341374a0da0890bf9d033f17539c8ff8
|
|
| MD5 |
e42f565caf85057196e8730958de3676
|
|
| BLAKE2b-256 |
3f28e3c9e91b2c6ed2c00975437482842f1d4d2e4c432dec6f0c8a9073a473ce
|
File details
Details for the file dataclass2PySide6-1.7.0-py3-none-any.whl.
File metadata
- Download URL: dataclass2PySide6-1.7.0-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcd64781482825443bb03819c25841aeffc7bb714f4dfc8d89ce8d859cf3e838
|
|
| MD5 |
e9d5c8513d72ae91f4aa7fab058e8f1b
|
|
| BLAKE2b-256 |
02218d58b9d5143f0e8b1652d1f4fc83ab1f3cfc7d08abd9dab2f2d1db355e3b
|