Skip to content

BaseFieldsModel

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
DataClassFieldsModel prettyqt.itemmodels.dataclassfieldsmodel

⋔ Inheritance diagram

graph TD
  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"]
  1473299901888 --> 1473245443072
  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\basefieldsmodel.py
class BaseFieldsModel(core.AbstractTableModel):
    HEADER: list[str] = []
    DELEGATE_DEFAULT = "editor"

    def __init__(self, instance, **kwargs):
        self._instance = instance
        self._fields = self.get_fields(instance)
        self._field_names = list(self._fields.keys())
        super().__init__(**kwargs)
        self.set_instance(instance)

    def get_fields(self, instance) -> dict[str, Any]:
        return NotImplemented

    def set_instance(self, instance):
        self._instance = instance
        self._fields = self.get_fields(instance)
        self._field_names = list(self._fields.keys())
        self.update_all()

    def columnCount(self, parent=None) -> int:
        return len(self.HEADER)

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

    def setData(
        self,
        index: core.ModelIndex,
        value: Any,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
    ) -> bool:
        if not index.isValid():
            return None
        match role, index.column():
            case constants.USER_ROLE | constants.EDIT_ROLE, _:
                field_name = self._field_names[index.row()]
                setattr(self._instance, field_name, value)
                self.update_row(index.row())
                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._field_names)

    def _is_writable(self, field_name: str) -> bool:
        """Dumb check, set same value and check if it works.

        Should be overridden by subclasses if possible.
        """
        val = getattr(self._instance, field_name)
        with contextlib.suppress(Exception):
            setattr(self._instance, field_name, val)
            return True
        return False

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        field_name = self._field_names[index.row()]
        if index.column() == 0:
            if self._is_writable(field_name):
                return super().flags(index) | constants.IS_EDITABLE
        return super().flags(index)

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

Override for AbstractitemModel base method.

Source code in prettyqt\itemmodels\basefieldsmodel.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._field_names)

Info

Recommended delegate: 'editor'

⌗ Property table

Qt Property Type Doc
objectName QString