Skip to content

MultiColumnFilterProxyModel

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
  1473290741344["itemmodels.MultiColumnFilterProxyModel"]
  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 --> 1473290741344
  1473299903840 --> 1473299898960
  1473299890176 --> 1473299903840
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289062816 --> 1473299898960
  1473289061840 --> 1473289062816
  1473289050128 --> 1473289061840
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: SortFilterProxyModel

A SortFilterProxyModel which filters based on multiple columns in one go.

This proxy uses seperate search term / values for each column in order to filter the source model, thus avoiding to layer proxy models in case you want to filter based on several columns. That way it is less demanding since filtering for all columns is done in one go.

This model is used by the FilterHeader widget in order to filter a table in one go, avoiding the need to layer multiple proxy models.

Example

proxy = itemmodels.MultiColumnFilterProxyModel()
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt\itemmodels\proxies\multicolumnfilterproxymodel.py
class MultiColumnFilterProxyModel(core.SortFilterProxyModel):
    """A SortFilterProxyModel which filters based on multiple columns in one go.

    This proxy uses seperate search term / values for each column in order to filter
    the source model, thus avoiding to layer proxy models in case you want to filter
    based on several columns. That way it is less demanding since filtering
    for all columns is done in one go.

    This model is used by the [FilterHeader](filterheader.md) widget in order to filter
    a table in one go, avoiding the need to layer multiple proxy models.


    ### Example

    ```py
    proxy = itemmodels.MultiColumnFilterProxyModel()
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "multi_column_filter"

    def __init__(self, parent=None, **kwargs):
        super().__init__(parent, **kwargs)
        self._filters: dict[int, Filter] = {}
        self.setRecursiveFilteringEnabled(True)

    def clear_filters(self):
        self._filters = {}
        self.invalidateRowsFilter()

    def set_filter_value(
        self,
        column: int,
        value: str,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if value == "" or value is None:  # False is a valid value.
            del self._filters[column]
        else:
            self._filters[column] = Filter(column=column, value=value, role=role)
        self.invalidateRowsFilter()

    def setFilterKeyColumn(self, column: int):
        raise NotImplementedError("Not supported.")

    def setFilterRole(self, column: int):
        raise NotImplementedError("Not supported.")

    def filterAcceptsRow(self, row, parent):
        source = self.sourceModel()
        for k, v in self._filters.items():
            index = source.index(row, k, parent)
            data = source.data(index, v.role)
            search_val = v.value
            match search_val:
                case str():
                    search_val = str(search_val)
                    data = str(data)
                    if self.filterMode == "fuzzy" and not fuzzy.fuzzy_match_simple(
                        search_val,
                        data,
                        case_sensitive=self.is_filter_case_sensitive(),
                    ):
                        return False
                    if not self.is_filter_case_sensitive():
                        search_val = search_val.lower()
                        data = data.lower()
                    if not data.startswith(search_val):
                        return False
                case Callable():
                    return search_val(data)
                case _:
                    if data != search_val:
                        return False
        return True

⌗ 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