Skip to content

BaseDataclassModel

Qt Base Class: QAbstractTableModel

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

Base classes

Name Children Inherits
AbstractTableModel
prettyqt.core.abstracttablemodel

Subclasses

Class Module Description
DataClassModel prettyqt.itemmodels.dataclassmodel

⋔ Inheritance diagram

graph TD
  1473245454784["itemmodels.BaseDataclassModel"]
  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"]
  1473299901888 --> 1473245454784
  1473299900912 --> 1473299901888
  1473299890176 --> 1473299900912
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289054032 --> 1473299901888
  1473289050128 --> 1473289054032
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: AbstractTableModel

Source code in prettyqt\itemmodels\basedataclassmodel.py
class BaseDataclassModel(core.AbstractTableModel):
    DELEGATE_DEFAULT = "editor"

    def __init__(self, items: Sequence, **kwargs):
        super().__init__(**kwargs)
        self.items = items
        klasses = [type(i) for i in items]
        self.Class = classhelpers.lca_type(klasses)
        logger.debug(f"{type(self).__name__}: found common ancestor {self.Class}")
        self._fields = self.get_fields()
        self._field_names = list(self._fields.keys())

    def get_fields(self):
        return NotImplemented

    def columnCount(self, parent=None):
        return len(self._fields)

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> str | None:
        match orientation, role, section:
            case constants.VERTICAL, constants.DISPLAY_ROLE, _:
                instance = self.items[section]
                return type(instance).__name__
            case constants.HORIZONTAL, constants.DISPLAY_ROLE, _:
                return self._field_names[section]

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not index.isValid():
            return None
        field_name = self._field_names[index.column()]
        instance = self.items[index.row()]
        value = getattr(instance, field_name)
        match role:
            case constants.DISPLAY_ROLE if not isinstance(value, bool):
                return repr(value)
            case constants.CHECKSTATE_ROLE if isinstance(value, bool):
                return self.to_checkstate(value)
            case constants.USER_ROLE | constants.EDIT_ROLE:
                return value

    def setData(
        self,
        index: core.ModelIndex,
        value: Any,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
    ) -> bool:
        field_name = self._field_names[index.column()]
        instance = self.items[index.row()]
        match role:
            case constants.EDIT_ROLE | constants.USER_ROLE:
                with self.reset_model():
                    setattr(instance, field_name, value)
                return True
            case constants.CHECKSTATE_ROLE:
                with self.reset_model():
                    setattr(instance, field_name, bool(value))
                return True
        return False

    def rowCount(self, parent: core.ModelIndex | None = None) -> int:
        """Override for AbstractitemModel base method."""
        parent = parent or core.ModelIndex()
        if parent.column() > 0:
            return 0
        return 0 if parent.isValid() else len(self.items)

    def flags(self, parent: core.ModelIndex) -> constants.ItemFlag:
        """Override.

        BaseClass implementation just tries to set attribute with same value to test
        if field is writable. If possible, subclasses should find a more efficient way.
        """
        if not parent.isValid():
            return super().flags(parent)
        field_name = self._field_names[parent.column()]
        instance = self.items[parent.row()]
        # need to cover not parent.isValid()?
        val = getattr(instance, field_name)
        with contextlib.suppress(Exception):
            setattr(instance, field_name, val)
            if isinstance(val, bool):
                return super().flags(parent) | constants.IS_CHECKABLE
            else:
                return super().flags(parent) | constants.IS_EDITABLE
        return super().flags(parent)

flags(parent: core.ModelIndex) -> constants.ItemFlag

Override.

BaseClass implementation just tries to set attribute with same value to test if field is writable. If possible, subclasses should find a more efficient way.

Source code in prettyqt\itemmodels\basedataclassmodel.py
def flags(self, parent: core.ModelIndex) -> constants.ItemFlag:
    """Override.

    BaseClass implementation just tries to set attribute with same value to test
    if field is writable. If possible, subclasses should find a more efficient way.
    """
    if not parent.isValid():
        return super().flags(parent)
    field_name = self._field_names[parent.column()]
    instance = self.items[parent.row()]
    # need to cover not parent.isValid()?
    val = getattr(instance, field_name)
    with contextlib.suppress(Exception):
        setattr(instance, field_name, val)
        if isinstance(val, bool):
            return super().flags(parent) | constants.IS_CHECKABLE
        else:
            return super().flags(parent) | constants.IS_EDITABLE
    return super().flags(parent)

rowCount(parent: core.ModelIndex | None = None) -> int

Override for AbstractitemModel base method.

Source code in prettyqt\itemmodels\basedataclassmodel.py
def rowCount(self, parent: core.ModelIndex | None = None) -> int:
    """Override for AbstractitemModel base method."""
    parent = parent or core.ModelIndex()
    if parent.column() > 0:
        return 0
    return 0 if parent.isValid() else len(self.items)

Info

Recommended delegate: 'editor'

⌗ Property table

Qt Property Type Doc
objectName QString