Skip to content

TupleTreeModel

Qt Base Class: QAbstractItemModel

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

Base classes

Name Children Inherits
TreeModel
prettyqt.itemmodels.treemodel

⋔ Inheritance diagram

graph TD
  1473245464544["itemmodels.TupleTreeModel"]
  1473299686192["itemmodels.TreeModel"]
  1473299893104["core.AbstractItemModel"]
  1473299890176["core.AbstractItemModelMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473289050128["QtCore.QAbstractItemModel"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473299686192 --> 1473245464544
  1473299893104 --> 1473299686192
  1473299890176 --> 1473299893104
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289050128 --> 1473299893104
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: TreeModel

Base Tree Model to display a dict[tuple[Any], str] data structure.

The dictionary keys are tuples of path parts, like ("path", "to", "something")

model = TupleTreeModel({("a"): "test2", ("a", "b"): "test", ("a", "b", "c"): "test3"})
table = widgets.TreeView()
table.set_model(model)
table.show()
Source code in prettyqt\itemmodels\tupletreemodel.py
class TupleTreeModel(itemmodels.TreeModel):
    """Base Tree Model to display a dict[tuple[Any], str] data structure.

    The dictionary keys are tuples of path parts, like ("path", "to", "something")

    ```py
    model = TupleTreeModel({("a"): "test2", ("a", "b"): "test", ("a", "b", "c"): "test3"})
    table = widgets.TreeView()
    table.set_model(model)
    table.show()
    ```
    """

    SUPPORTS = dict[tuple[Hashable, ...] | pathlib.Path, str]
    HEADER = ["Name"]

    def __init__(self, mapping: dict, **kwargs):
        super().__init__((), **kwargs)
        self.mapping = {(): "root"} | mapping

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

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

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not index.isValid():
            return None
        tup = self.data_by_index(index).obj
        match role, index.column():
            case constants.DISPLAY_ROLE, 0:
                return str(self.mapping[tup])

    @classmethod
    def supports(cls, instance) -> bool:
        match instance:
            case dict() if all(
                isinstance(k, pathlib.Path | tuple) for k in instance.keys() and instance
            ):
                return True
        return False

    def _fetch_object_children(
        self, item: TupleTreeModel.TreeItem
    ) -> list[TupleTreeModel.TreeItem]:
        parts = item.obj.parts if isinstance(item.obj, pathlib.Path) else item.obj
        return [
            self.TreeItem(obj=k)
            for k in self.mapping.keys()
            if len(k) == len(parts) + 1
            and all(parent_part == k[i] for i, parent_part in enumerate(parts))
        ]

    def _has_children(self, item: TupleTreeModel.TreeItem) -> bool:
        parts = item.obj.parts if isinstance(item.obj, pathlib.Path) else item.obj
        return any(
            len(k) == len(parts) + 1
            and all(parent_part == k[i] for i, parent_part in enumerate(parts))
            for k in self.mapping.keys()
        )

Info

Supported data type: dict[tuple[collections.abc.Hashable, ...] | pathlib.Path, str]

⌗ Property table

Qt Property Type Doc
objectName QString