Skip to content

WidgetsDetailsModel

Qt Base Class: QAbstractTableModel

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

Base classes

Name Children Inherits
AbstractTableModel
prettyqt.core.abstracttablemodel

⋔ Inheritance diagram

graph TD
  1473290709136["itemmodels.WidgetsDetailsModel"]
  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 --> 1473290709136
  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\widgetsdetailsmodel.py
class WidgetsDetailsModel(core.AbstractTableModel):
    SUPPORTS = Sequence[core.QObject]

    def __init__(self, items: Sequence[core.QObject], **kwargs):
        super().__init__(**kwargs)
        self.items = items
        common_ancestor = classhelpers.find_common_ancestor([type(i) for i in self.items])
        logger.debug(f"{type(self).__name__}: found common ancestor {common_ancestor}")
        self.props = core.MetaObject(common_ancestor.staticMetaObject).get_properties(
            only_stored=True
        )
        self.props.sort(key=lambda x: x.get_name())

    @classmethod
    def supports(cls, instance) -> bool:
        match instance:
            case (core.QObject(), *_):
                return True
            case _:
                return False

    def columnCount(self, parent: core.ModelIndex | None = None):
        return len(self.props)

    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, _:
                widget = self.items[section]
                return repr(widget)
            case constants.HORIZONTAL, constants.DISPLAY_ROLE, _:
                return self.props[section].get_name()

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not index.isValid():
            return None
        prop = self.props[index.column()]
        widget = self.items[index.row()]
        value = prop.read(widget)
        if isinstance(value, bool) and role == constants.CHECKSTATE_ROLE:
            return self.to_checkstate(value)
        match role:
            case (
                constants.DISPLAY_ROLE | constants.EDIT_ROLE | constants.USER_ROLE
            ) if not isinstance(value, bool):
                return value

    def setData(
        self,
        index: core.ModelIndex,
        value,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
    ) -> bool:
        prop = self.props[index.column()]
        widget = self.items[index.row()]
        match role:
            case constants.USER_ROLE | constants.EDIT_ROLE:
                with self.reset_model():
                    prop.write(widget, value)
                # self.update_row(index.row())
                return True
            case constants.CHECKSTATE_ROLE:
                with self.reset_model():
                    prop.write(widget, bool(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.items)

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        prop = self.props[index.column()]
        if prop.isWritable() and prop.get_python_type() is bool:
            return super().flags(index) | constants.IS_CHECKABLE
        if prop.isWritable():
            return super().flags(index) | constants.IS_EDITABLE
        return constants.IS_SELECTABLE

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

Override for AbstractitemModel base method.

Source code in prettyqt\itemmodels\widgetsdetailsmodel.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

Supported data type: collections.abc.Sequence[PySide6.QtCore.QObject]

⌗ Property table

Qt Property Type Doc
objectName QString