Skip to main content

Python package to dynamically create PySide6 widgets from dataclass

:warning: This package no longer actively maintained. Use DaWiQ instead.

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:

  1. Enum : converted to combo box
  2. bool : converted to check box (Union with None allowed)
  3. int and float : converted to line edit with validator (Union with None allowed)
  4. str : converted to line edit
  5. Tuple : 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.

Note that changing the current index (or current widget) does not emit dataValuechanged signal. Listen to standard currentChanged signal to detect it.

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.


Metadata

Release files for dataclass2PySide6 1.8.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dataclass2PySide6 1.8.4
File Size Uploaded
dataclass2PySide6-1.8.4.tar.gz 17.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dataclass2PySide6 1.8.4
File Interpreter ABI Platform
dataclass2PySide6-1.8.4-py3-none-any.whl Python 3 none any Details

Total release size: 36.6 kB

Release files / dataclass2PySide6-1.8.4.tar.gz

Download URL dataclass2PySide6-1.8.4.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
040e6603caefe729a123ce747a0facb995e08969abfb833a7faf44449e2c5652
BLAKE2b-256 checksum
How to use checksums
1ff472d0cea139833a911b3810c5dc13456dfba1ed9416dd4ae9979330a018e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release files / dataclass2PySide6-1.8.4-py3-none-any.whl

Download URL dataclass2PySide6-1.8.4-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a544533b54bb13df39d6087b071929cb84d72871c66aa5db1bbce387dba8f67
BLAKE2b-256 checksum
How to use checksums
3f0d53473e2b44b2265f6cc2dc80d0d7e2392a82c4c601aa504b7ecc26cad8ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release history Release notifications | RSS feed

This release

1.8.4 This release

2 release files

1.8.3

2 release files

1.8.2

2 release files

1.8.1

2 release files

1.8.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page