Skip to content

XmlModel

Qt Base Class: QAbstractItemModel

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

Base classes

Name Children Inherits
BaseXmlModel
prettyqt.itemmodels.xmlmodel

⋔ Inheritance diagram

graph TD
  1473245460640["itemmodels.XmlModel"]
  1473245482112["itemmodels.BaseXmlModel"]
  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"]
  1473245482112 --> 1473245460640
  1473245677312 --> 1473245482112
  1473245682192 --> 1473245677312
  140713234304496 --> 1473245682192
  1473299686192 --> 1473245677312
  1473299893104 --> 1473299686192
  1473299890176 --> 1473299893104
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289050128 --> 1473299893104
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: BaseXmlModel

Semi-lazy xml model. Fetches all direct child nodes when needed.

Model cant be modified, that only really makes sense for a full DOM implementation.

Source code in prettyqt\itemmodels\xmlmodel.py
class XmlModel(BaseXmlModel):
    """Semi-lazy xml model. Fetches all direct child nodes when needed.

    Model cant be modified, that only really makes sense for a full DOM implementation.
    """

    SUPPORTS = io.StringIO | str | datatypes.IsTreeIterator | ET.ElementTree

    def __init__(
        self,
        obj: str | datatypes.IsTreeIterator | ET.ElementTree,
        show_root: bool = True,
        **kwargs,
    ):
        match obj:
            case io.StringIO():
                context = ET.iterparse(obj, events=("start",))
                _, root = next(context)
            case str():
                context = ET.iterparse(io.StringIO(obj), events=("start",))
                _, root = next(context)
            case datatypes.IsTreeIterator():
                _, root = next(obj)
            case ET.ElementTree():
                xml_str = ET.tostring(obj._root, encoding="unicode")
                context = ET.iterparse(io.StringIO(xml_str), events=("start",))
                _, root = next(context)
            case _:
                raise TypeError(obj)
        super().__init__(obj=root, **kwargs)

    @classmethod
    def supports(cls, instance) -> bool:
        match instance:
            case datatypes.IsTreeIterator() | ET.ElementTree():
                return True
            case _:
                return False

    def get_parent_node(self, node_or_index: core.ModelIndex | ET.Element):
        # only lxml has Element.getparent()
        # for builtin Elements we need to go through indexes since we do not have a
        # full ElementTree.
        # might be worth having two XML models, a lazy one and a full-featured one.
        # Then we could use xpath to get parent here.
        if isinstance(node_or_index, core.ModelIndex):
            index = node_or_index
        elif indexes := self.search_tree(
            node_or_index, role=self.Roles.NodeRole, max_results=1
        ):
            index = indexes[0]
        else:
            return None
        parent = index.parent()
        return parent.data(self.Roles.NodeRole)

Info

Supported data type: _io.StringIO | str | prettyqt.utils.datatypes.IsTreeIterator | xml.etree.ElementTree.ElementTree

⌗ Property table

Qt Property Type Doc
objectName QString