Skip to content

JsonModel

Qt Base Class: QAbstractItemModel

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

Base classes

Name Children Inherits
ColumnItemModel
prettyqt.itemmodels.columnitemmodel

⋔ Inheritance diagram

graph TD
  1473245451856["itemmodels.JsonModel"]
  1473245677312["itemmodels.ColumnItemModel"]
  1473245682192["itemmodels.ColumnItemModelMixin"]
  140713234304496["builtins.object"]
  1473299686192["itemmodels.TreeModel"]
  1473299893104["core.AbstractItemModel"]
  1473299890176["core.AbstractItemModelMixin"]
  1473299815024["core.ObjectMixin"]
  1473289050128["QtCore.QAbstractItemModel"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473245677312 --> 1473245451856
  1473245682192 --> 1473245677312
  140713234304496 --> 1473245682192
  1473299686192 --> 1473245677312
  1473299893104 --> 1473299686192
  1473299890176 --> 1473299893104
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289050128 --> 1473299893104
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: ColumnItemModel

Source code in prettyqt\itemmodels\jsonmodel.py
class JsonModel(itemmodels.ColumnItemModel):
    COLUMNS = [NameColumn, ValueColumn, TypeColumn]
    SUPPORTS = Mapping

    def __init__(
        self,
        obj: Any,
        show_root: bool = True,
        **kwargs,
    ):
        super().__init__(
            obj=JsonItem(key="", value=obj, typ=type(obj)),
            columns=self.COLUMNS,
            show_root=show_root,
            **kwargs,
        )

    @classmethod
    def supports(cls, instance) -> bool:
        return isinstance(instance, Mapping)

    def _has_children(self, item: JsonModel.TreeItem) -> bool:
        return isinstance(item.obj.value, dict | list | set) and bool(item.obj.value)

    def _fetch_object_children(
        self, item: JsonModel.TreeItem
    ) -> list[JsonModel.TreeItem]:
        """Fetch the children of a Python object.

        Returns: list of treeitem.TreeItems
        """
        # items = []
        match item.obj.value:
            case Mapping():
                return [
                    self.TreeItem(obj=JsonItem(key=k, value=v, typ=type(v)))
                    for k, v in item.obj.value.items()
                ]

            case Iterable() if not isinstance(item.obj.value, str):
                return [
                    self.TreeItem(obj=JsonItem(key=k, value=v, typ=type(v)))
                    for k, v in enumerate(item.obj.value)
                ]
            case _:
                return [
                    self.TreeItem(
                        obj=JsonItem(
                            key="key",
                            value=repr(item.obj.value),
                            typ=type(item.obj.value),
                        )
                    )
                ]

Info

Supported data type: <class 'collections.abc.Mapping'>

⌗ Property table

Qt Property Type Doc
objectName QString