Skip to content

ParentClassTreeModel

Qt Base Class: QAbstractItemModel

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

Base classes

Name Children Inherits
BaseClassTreeModel
prettyqt.itemmodels.classtreemodel

⋔ Inheritance diagram

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

🛈 DocStrings

Bases: BaseClassTreeModel

Tree model to display the parent class tree of a python class.

from prettyqt import itemmodels, widgets

app = widgets.app()
widget = widgets.TreeView()
model = itemmodels.ParentClassTreeModel(widgets.TreeWidget)
widget.set_model(model)
widget.show()

Image title

from prettyqt import itemmodels, widgets

app = widgets.app()
widget = widgets.TreeView()
model = itemmodels.ParentClassTreeModel(widgets.TreeWidget, mro=True)
widget.set_model(model)
widget.show()

Image title

Source code in prettyqt\itemmodels\classtreemodel.py
class ParentClassTreeModel(BaseClassTreeModel):
    """Tree model to display the parent class tree of a python class.

    === "Parentclass tree"

        ```py
        from prettyqt import itemmodels, widgets

        app = widgets.app()
        widget = widgets.TreeView()
        model = itemmodels.ParentClassTreeModel(widgets.TreeWidget)
        widget.set_model(model)
        widget.show()
        ```

        <figure markdown>
          ![Image title](treewidget_parentclasses.png)
        </figure>

    === "MRO tree"

        ```py
        from prettyqt import itemmodels, widgets

        app = widgets.app()
        widget = widgets.TreeView()
        model = itemmodels.ParentClassTreeModel(widgets.TreeWidget, mro=True)
        widget.set_model(model)
        widget.show()
        ```

        <figure markdown>
          ![Image title](treewidget_mro.png)
        </figure>
    """

    SUPPORTS = type

    def __init__(self, *args, **kwargs):
        self._show_mro = False
        super().__init__(*args, **kwargs)

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

    def _fetch_object_children(
        self, item: ParentClassTreeModel.TreeItem
    ) -> list[ParentClassTreeModel.TreeItem]:
        if self._show_mro:
            return [self.TreeItem(obj=i) for i in item.obj.mro()[1:]]
        else:
            return [self.TreeItem(obj=i) for i in item.obj.__bases__]

    def _has_children(self, item: ParentClassTreeModel.TreeItem) -> bool:
        if item.obj is None:
            return False
        return len(item.obj.__bases__) > 0

    def set_show_mro(self, show: bool):
        """Toggles mro mode on or off.

        Arguments:
            show: toggle mro mode on/off
        """
        self._show_mro = show

    def get_show_mro(self) -> bool:
        """Returns True if mro mode is turned on."""
        return self._show_mro

    show_mro = core.Property(
        bool,
        get_show_mro,
        set_show_mro,
        doc="Whether children items should be the ordered mro.",
    )

get_show_mro() -> bool

Returns True if mro mode is turned on.

Source code in prettyqt\itemmodels\classtreemodel.py
def get_show_mro(self) -> bool:
    """Returns True if mro mode is turned on."""
    return self._show_mro

set_show_mro(show: bool)

Toggles mro mode on or off.

Parameters:

Name Type Description Default
show bool

toggle mro mode on/off

required
Source code in prettyqt\itemmodels\classtreemodel.py
def set_show_mro(self, show: bool):
    """Toggles mro mode on or off.

    Arguments:
        show: toggle mro mode on/off
    """
    self._show_mro = show

Info

Supported data type: <class 'type'>

⌗ Property table

Qt Property Type Doc
objectName QString
show_mro bool Whether children items should be the ordered mro.