The qt-pydantic
package adds support for Qt types in Pydantic BaseModels.
Using these annotations allows for easy serialization and deserialization of Qt types.
Install using pip:
pip install qt-pydantic
from PySide6 import QtCore, QtGui
from pydantic import BaseModel
from qt_pydantic import QSize, QColor, QDate
# Define a model with Qt types
class Settings(BaseModel):
size: QSize
date: QDate
color: QColor
# Parse json string into model
json_data = '{"size": [720, 480], "date": "2021-01-01", "color": [255, 95, 135]}'
settings = Settings.model_validate_json(json_data)
# Model types are actual Qt types
assert isinstance(settings.size, QtCore.QSize)
assert isinstance(settings.date, QtCore.QDate)
assert isinstance(settings.color, QtGui.QColor)
# Turn model into json string
data = settings.model_dump_json(indent=2)
To contribute please refer to the Contributing Guide.
MIT License. Copyright 2024 - Beat Reichenbach. See the License file for details.