Skip to content

PredicateFilterProxyModel

Qt Base Class: QSortFilterProxyModel

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

Base classes

Name Children Inherits
SortFilterProxyModel
prettyqt.core.sortfilterproxymodel

⋔ Inheritance diagram

graph TD
  1473290747200["itemmodels.PredicateFilterProxyModel"]
  1473299898960["core.SortFilterProxyModel"]
  1473299903840["core.AbstractProxyModelMixin"]
  1473299890176["core.AbstractItemModelMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473289062816["QtCore.QSortFilterProxyModel"]
  1473289061840["QtCore.QAbstractProxyModel"]
  1473289050128["QtCore.QAbstractItemModel"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473299898960 --> 1473290747200
  1473299903840 --> 1473299898960
  1473299890176 --> 1473299903840
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289062816 --> 1473299898960
  1473289061840 --> 1473289062816
  1473289050128 --> 1473289061840
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: SortFilterProxyModel

A simple filter proxy model with settable filter predicates.

Example:

proxy = PredicateFilterProxyModel()
proxy.add_filter(lambda value: value < 1)
Source code in prettyqt\itemmodels\proxies\predicatefilterproxymodel.py
class PredicateFilterProxyModel(core.SortFilterProxyModel):
    """A simple filter proxy model with settable filter predicates.

    ### Example:
    ```py
    proxy = PredicateFilterProxyModel()
    proxy.add_filter(lambda value: value < 1)
    ```
    """

    ID = "predicate_filter"

    def __init__(self, parent=None, **kwargs):
        super().__init__(parent, **kwargs)
        self._filters: list[Callable[[Any], bool]] = []

    def clear_filters(self):
        self._filters = []
        self.invalidateFilter()

    def add_filter(self, predicate: Callable[[Any], bool]):
        self._filters.append(predicate)
        self.invalidateFilter()

    def filterAcceptsRow(self, row, parent):
        source = self.sourceModel()
        col = self.filterKeyColumn()
        role = self.filterRole()
        index = source.index(row, col, parent)
        data = source.data(index, role)

        def apply(f: Callable):
            try:
                return f(data)
            except (TypeError, ValueError):
                return False

        return all(apply(f) for f in self._filters)

⌗ Property table

Qt Property Type Doc
objectName QString
sourceModel QAbstractItemModel
filterRegularExpression QRegularExpression
filterKeyColumn int
dynamicSortFilter bool
filterCaseSensitivity Qt::CaseSensitivity
sortCaseSensitivity Qt::CaseSensitivity
isSortLocaleAware bool
sortRole int
filterRole int
recursiveFilteringEnabled bool
autoAcceptChildRows bool
filterMode QString Mode to use for filtering