Skip to content

DataClassFieldsModel

Qt Base Class: QAbstractTableModel

Signature: QAbstractTableModel(self, parent: Optional[PySide6.QtCore.QObject] = None) -> None

Base classes

Name Children Inherits
BaseFieldsModel
prettyqt.itemmodels.basefieldsmodel

⋔ Inheritance diagram

graph TD
  1473245456736["itemmodels.DataClassFieldsModel"]
  1473245443072["itemmodels.BaseFieldsModel"]
  1473299901888["core.AbstractTableModel"]
  1473299900912["core.AbstractTableModelMixin"]
  1473299890176["core.AbstractItemModelMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473289054032["QtCore.QAbstractTableModel"]
  1473289050128["QtCore.QAbstractItemModel"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473245443072 --> 1473245456736
  1473299901888 --> 1473245443072
  1473299900912 --> 1473299901888
  1473299890176 --> 1473299900912
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289054032 --> 1473299901888
  1473289050128 --> 1473289054032
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: BaseFieldsModel

Table model to display the fields and their metadata of an dataclass.

Frozen dataclasses are read-only, otherwise the data can be modified.

Source code in prettyqt\itemmodels\dataclassfieldsmodel.py
class DataClassFieldsModel(itemmodels.BaseFieldsModel):
    """Table model to display the fields and their metadata of an dataclass.

    Frozen dataclasses are read-only, otherwise the data can be modified.
    """

    SUPPORTS = datatypes.IsDataclass
    HEADER = [
        "Field name",
        "Value",
        "Type",
        "Default",
        "In __init__",
        "In __repr__",
        "Compare",
        "Hash",
        "Metadata",
        "Keyword only",
    ]

    def __init__(self, instance: datatypes.IsDataclass, **kwargs):
        super().__init__(instance, **kwargs)

    @classmethod
    def supports(cls, instance) -> bool:
        return isinstance(instance, datatypes.IsDataclass)

    def get_fields(self, instance: datatypes.IsDataclass) -> dict[str, Any]:
        fields = dataclasses.fields(instance)
        return {field.name: field for field in fields}

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not index.isValid():
            return None
        field_name = self._field_names[index.row()]
        field = self._fields[field_name]
        value = getattr(self._instance, field_name)
        match role, index.column():
            case constants.FONT_ROLE, 0:
                font = QtGui.QFont()
                font.setBold(True)
                return font
            case constants.DISPLAY_ROLE, 0:
                return repr(value)
            case constants.EDIT_ROLE, 0:
                return value
            case constants.DISPLAY_ROLE, 1:
                return field.type
            case constants.FONT_ROLE, 1:
                font = QtGui.QFont()
                font.setItalic(True)
                return font
            case constants.DISPLAY_ROLE, 2:
                return field.default
            case constants.CHECKSTATE_ROLE, 3:
                return self.to_checkstate(field.init)
            case constants.CHECKSTATE_ROLE, 4:
                return self.to_checkstate(field.repr)
            case constants.CHECKSTATE_ROLE, 5:
                return self.to_checkstate(field.compare)
            case constants.CHECKSTATE_ROLE, 6:
                return self.to_checkstate(field.hash)
            case constants.DISPLAY_ROLE, 7:
                return str(field.metadata)
            case constants.CHECKSTATE_ROLE, 8:
                return self.to_checkstate(field.kw_only)
            case constants.USER_ROLE, _:
                return value

    def _is_writable(self, field_name: str) -> bool:
        return not self._instance.__dataclass_params__.frozen

Info

Recommended delegate: 'editor'

Info

Supported data type: <class 'prettyqt.utils.datatypes.IsDataclass'>

⌗ Property table

Qt Property Type Doc
objectName QString