Skip to content

AbstractProxyModelMixin

Sub classes

Name Children Inherits
AbstractProxyModel
prettyqt.core.abstractproxymodel
Base class for proxy item models that can do sorting, filtering and processing.
IdentityProxyModel
prettyqt.core.identityproxymodel
Proxies its source model unmodified.
SortFilterProxyModel
prettyqt.core.sortfilterproxymodel
Support for sorting and filtering data passed between another model and a view.
TransposeProxyModel
prettyqt.core.transposeproxymodel
This proxy transposes the source model.

    Base classes

    Name Children Inherits
    AbstractItemModelMixin
    prettyqt.core.abstractitemmodel
    object
    builtins
    The base class of the class hierarchy.
    • type
    • async_generator
    • bytearray_iterator
    • bytearray
    • bytes_iterator
    • bytes
    • builtin_function_or_method
    • callable_iterator
    • PyCapsule
    • cell
    • classmethod_descriptor
    • ...

      â‹” Inheritance diagram

      graph TD
        2431222121["core.AbstractProxyModelMixin"]
        3882849576["core.AbstractItemModelMixin"]
        3227531497["core.ObjectMixin"]
        2751680898["builtins.object"]
        3882849576 --> 2431222121
        3227531497 --> 3882849576
        2751680898 --> 3227531497
        2751680898 --> 3882849576
        2751680898 --> 2431222121

      🛈 DocStrings

      Bases: AbstractItemModelMixin, _AbstractProxyModelBase

      Source code in prettyqt/core/abstractproxymodel.py
      class AbstractProxyModelMixin(core.AbstractItemModelMixin, _AbstractProxyModelBase):
          ID = ""
          ICON = "mdi.table-edit"
      
          def __pretty__(self, fmt: Callable[[Any], Any], **kwargs: Any) -> Generator[Any]:
              yield f"{type(self).__name__}("
              yield 1
              # yield 1
              yield f"objectName={self.objectName()}"
              yield 0
              for k, v in self.get_properties(include_super=False).items():
                  yield f"{k}={v!r}"
                  yield 0
              model = self.sourceModel()
              if (pretty := getattr(model, "__pretty__", None)) is not None:
                  yield from pretty(fmt, **kwargs)
                  yield 0
              yield -1
              # yield -1
              yield ")"
      
          def parent(self, *args):
              # workaround: PyQt6 QIdentityproxymodel.parent() missing
              if not args and qt.API == "pyqt6":
                  return core.QAbstractProxyModel.parent(self)
              return super().parent(*args)
      
          def first_item_index(self) -> core.ModelIndex:
              """Return the first child of the root item."""
              # We cannot just call the same function of the source model because the first node
              # there may be hidden.
              proxy_root_index = self.mapFromSource(core.ModelIndex())
              return self.index(0, 0, proxy_root_index)
      
          def get_source_model(self, skip_proxies: bool = True):
              model = self.sourceModel()
              if skip_proxies:
                  while isinstance(model, core.QAbstractProxyModel):
                      model = model.sourceModel()
              return model
      
          def remove(self):
              parent = self.parent()
              models = parent.get_models()
              idx = models.index(self)
              if idx == len(models) - 1:
                  parent.set_model(models[idx - 1])
                  self.setSourceModel(None)  # ty: ignore[invalid-argument-type]  # stub lacks None
              elif idx == 0 and len(models) > 0:
                  parent.set_model(models[1])
              elif idx > 0 and len(models) > 2:  # noqa: PLR2004
                  models[idx - 1].setSourceModel(models[idx + 1])
                  self.setSourceModel(None)  # ty: ignore[invalid-argument-type]  # stub lacks None
      
          def get_source_mapping(self, leaves_only: bool = False):
              _source_key = []
              _source_offset = {}
              source = self.sourceModel()
      
              def create_mapping(
                  model,
                  index: core.ModelIndex,
                  key_path: tuple[int, ...],
                  leaves_only: bool = False,
              ):
                  if (rowcount := model.rowCount(index)) > 0:
                      if not leaves_only:
                          _source_offset[key_path] = len(_source_offset)
                          _source_key.append(key_path)
                      for i in range(rowcount):
                          child = model.index(i, 0, index)
                          create_mapping(model, child, (*key_path, i), leaves_only=leaves_only)
                  else:
                      _source_offset[key_path] = len(_source_offset)
                      _source_key.append(key_path)
      
              for i in range(source.rowCount()):
                  create_mapping(source, source.index(i, 0), (i,), leaves_only=leaves_only)
              return _source_key, _source_offset
      
          def source_index_from_key(
              self,
              key_path: Sequence[tuple[int, int] | int],
              parent_index: core.ModelIndex | None = None,
          ) -> core.ModelIndex:
              """Return a QModelIndex of the sourceModel for the given key.
      
              Arguments:
                  key_path: Key path to get an index for.
                            Should be a sequence of either (row, column)  or row indices
                  parent_index: ModelIndex to start indexing from. Defaults to root index.
              """
              model = self.sourceModel()
              return modelhelpers.index_from_key(model, key_path, parent_index)
      

      first_item_index() -> core.ModelIndex

      Return the first child of the root item.

      Source code in prettyqt/core/abstractproxymodel.py
      def first_item_index(self) -> core.ModelIndex:
          """Return the first child of the root item."""
          # We cannot just call the same function of the source model because the first node
          # there may be hidden.
          proxy_root_index = self.mapFromSource(core.ModelIndex())
          return self.index(0, 0, proxy_root_index)
      

      source_index_from_key(key_path: Sequence[tuple[int, int] | int], parent_index: core.ModelIndex | None = None) -> core.ModelIndex

      Return a QModelIndex of the sourceModel for the given key.

      Parameters:

      Name Type Description Default
      key_path Sequence[tuple[int, int] | int]

      Key path to get an index for. Should be a sequence of either (row, column) or row indices

      required
      parent_index ModelIndex | None

      ModelIndex to start indexing from. Defaults to root index.

      None
      Source code in prettyqt/core/abstractproxymodel.py
      def source_index_from_key(
          self,
          key_path: Sequence[tuple[int, int] | int],
          parent_index: core.ModelIndex | None = None,
      ) -> core.ModelIndex:
          """Return a QModelIndex of the sourceModel for the given key.
      
          Arguments:
              key_path: Key path to get an index for.
                        Should be a sequence of either (row, column)  or row indices
              parent_index: ModelIndex to start indexing from. Defaults to root index.
          """
          model = self.sourceModel()
          return modelhelpers.index_from_key(model, key_path, parent_index)
      

      :fa-brands-github: Show source on GitHub