Skip to content

ImportlibTreeModel

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
  1473245450880["itemmodels.ImportlibTreeModel"]
  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 --> 1473245450880
  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

Model showing the dependency tree of a distribution.

Source code in prettyqt\itemmodels\importlibdistributionmodel.py
class ImportlibTreeModel(itemmodels.ColumnItemModel):
    """Model showing the dependency tree of a distribution."""

    @core.Enum
    class Roles(enum.IntEnum):
        DistributionRole = constants.USER_ROLE + 43255

    SUPPORTS = metadata.Distribution | str
    TreeItem = DistTreeItem
    IS_RECURSIVE = True
    COLUMNS = [
        NameColumn,
        VersionColumn,
        ConstraintsColumn,
        MarkerColumn,
        SummaryColumn,
        HomepageColumn,
        AuthorColumn,
        LicenseColumn,
    ]

    def __init__(
        self,
        obj: metadata.Distribution | str,
        show_root: bool = False,
        parent: core.QObject | None = None,
    ):
        if isinstance(obj, str):
            obj = metadata.distribution(obj)
        super().__init__(
            obj=obj, columns=self.COLUMNS, parent=parent, show_root=show_root
        )

    @classmethod
    def setup_example(cls):
        from prettyqt import widgets

        model = cls("prettyqt")
        table = widgets.TreeView(word_wrap=False)
        table.set_model(model)
        table.set_delegate("render_link", column=5)
        table.expand_all(depth=4)
        return table

    @classmethod
    def supports(cls, instance) -> bool:
        return isinstance(instance, metadata.Distribution)

    @classmethod
    def from_system(cls, parent: core.QObject | None = None) -> Self:
        distributions = list_system_modules()
        return cls(distributions, parent)

    @classmethod
    def from_package(cls, package_name: str, parent: core.QObject | None = None) -> Self:
        distributions = list_package_requirements(package_name)
        return cls(distributions, parent)

    def _has_children(self, item: ImportlibTreeModel.TreeItem) -> bool:
        return bool(item.requires)

    def _fetch_object_children(self, item: DistTreeItem) -> list[DistTreeItem]:
        return [
            DistTreeItem(obj=dist, parent=item)
            for dist in list_package_requirements(item.metadata["Name"])
        ]

Warning

Model can be recursive, so be careful with iterating whole tree.

Info

Supported data type: importlib.metadata.Distribution | str

⌗ Property table

Qt Property Type Doc
objectName QString

🖼 Screenshot

prettyqt.itemmodels.importlibdistributionmodel.ImportlibTreeModel.setup_example
@classmethod
def setup_example(cls):
    from prettyqt import widgets

    model = cls("prettyqt")
    table = widgets.TreeView(word_wrap=False)
    table.set_model(model)
    table.set_delegate("render_link", column=5)
    table.expand_all(depth=4)
    return table

Image title