Skip to content

SliceFilterProxyModel

Qt Base Class: QIdentityProxyModel

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

Base classes

Name Children Inherits
SliceIdentityProxyModel
prettyqt.itemmodels.proxies.sliceidentityproxymodel

⋔ Inheritance diagram

graph TD
  1473290718896["itemmodels.SliceFilterProxyModel"]
  1473290716944["itemmodels.SliceIdentityProxyModel"]
  1473299892128["core.IdentityProxyModel"]
  1473299903840["core.AbstractProxyModelMixin"]
  1473299890176["core.AbstractItemModelMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473289064768["QtCore.QIdentityProxyModel"]
  1473289061840["QtCore.QAbstractProxyModel"]
  1473289050128["QtCore.QAbstractItemModel"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473290716944 --> 1473290718896
  1473299892128 --> 1473290716944
  1473299903840 --> 1473299892128
  1473299890176 --> 1473299903840
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289064768 --> 1473299892128
  1473289061840 --> 1473289064768
  1473289050128 --> 1473289061840
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: SliceIdentityProxyModel

Proxy model to filter an item view based on python slicing syntax.

Since slicing operations are bijective, this model can filter without looping through rows or columns. Thus, this should perform much better than a SortFilterProxyModel with a column filter. (O(1) instead of O(n))

Example

To filter out every second row, and cut off the the first two columns:

model = MyModel()
table = widgets.TableView()
table.set_model(model)
table.proxifier[::2, 2:].filter()
table.show()
# or
indexer = (slice(None, None, 2), slice(2, None))
proxy = itemmodels.SliceFilterProxyModel(indexer=indexer)
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt\itemmodels\proxies\slicefilterproxymodel.py
class SliceFilterProxyModel(itemmodels.SliceIdentityProxyModel):
    """Proxy model to filter an item view based on python slicing syntax.

    Since slicing operations are bijective, this model can filter without
    looping through rows or columns. Thus, this should perform much better than a
    SortFilterProxyModel with a column filter. (O(1) instead of O(n))

    ### Example

    To filter out every second row, and cut off the the first two columns:

    ```py
    model = MyModel()
    table = widgets.TableView()
    table.set_model(model)
    table.proxifier[::2, 2:].filter()
    table.show()
    # or
    indexer = (slice(None, None, 2), slice(2, None))
    proxy = itemmodels.SliceFilterProxyModel(indexer=indexer)
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "slice_filter"
    ICON = "mdi.table-filter"

    def rowCount(self, index: core.ModelIndex | None = None) -> int:
        rowcount = super().rowCount()
        # TODO: not sure if slice.stop = 0 is covered correctly?
        return min(rowcount, self.get_row_slice().stop or rowcount)

    def columnCount(self, index: core.ModelIndex | None = None) -> int:
        colcount = super().columnCount()
        return min(colcount, self.get_column_slice().stop or colcount)

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        """Map header data to proxy by calculating position from slice values.

        source pos = slice start + proxy pos * slice step)
        """
        is_horizontal = orientation == constants.HORIZONTAL
        rng = self.get_column_range() if is_horizontal else self.get_row_range()
        pos = rng.start + section * rng.step
        return super().headerData(pos, orientation, role)

    def index(
        self, row: int, column: int, parent: core.ModelIndex | None = None
    ) -> core.ModelIndex:
        parent = parent or core.ModelIndex()
        source = self.sourceModel()
        if row < 0 or column < 0 or source is None:
            return core.ModelIndex()
        source_parent = self.mapToSource(parent)
        col_range = self.get_column_range()
        row_range = self.get_row_range()
        col_pos = col_range.start + (col_range.step * column)
        row_pos = row_range.start + (row_range.step * row)
        source_index = source.index(row_pos, col_pos, source_parent)
        return self.mapFromSource(source_index)

    def mapToSource(self, proxy_idx: core.ModelIndex) -> core.ModelIndex:
        """Map index to source by calculating position from slice values.

        source pos = slice start + proxy pos * slice step)
        """
        source = self.sourceModel()
        if source is None or not proxy_idx.isValid():
            return core.ModelIndex()
        col_range = self.get_column_range()
        row_range = self.get_row_range()
        col_pos = col_range.start + (col_range.step * proxy_idx.column())
        row_pos = row_range.start + (row_range.step * proxy_idx.row())
        return source.index(row_pos, col_pos)

    def mapFromSource(self, source_index: core.ModelIndex) -> core.ModelIndex:
        """Map index from source by calculating position based on slice values.

        proxy pos = source pos - slice start / slice step
        """
        if self.sourceModel() is None or not source_index.isValid():
            return core.ModelIndex()
        row_pos = self.position_in_row_slice(source_index.row())
        col_pos = self.position_in_column_slice(source_index.column())
        return self.createIndex(row_pos, col_pos, source_index.internalPointer())

headerData(section: int, orientation: constants.Orientation, role: constants.ItemDataRole = constants.DISPLAY_ROLE)

Map header data to proxy by calculating position from slice values.

source pos = slice start + proxy pos * slice step)

Source code in prettyqt\itemmodels\proxies\slicefilterproxymodel.py
def headerData(
    self,
    section: int,
    orientation: constants.Orientation,
    role: constants.ItemDataRole = constants.DISPLAY_ROLE,
):
    """Map header data to proxy by calculating position from slice values.

    source pos = slice start + proxy pos * slice step)
    """
    is_horizontal = orientation == constants.HORIZONTAL
    rng = self.get_column_range() if is_horizontal else self.get_row_range()
    pos = rng.start + section * rng.step
    return super().headerData(pos, orientation, role)

mapFromSource(source_index: core.ModelIndex) -> core.ModelIndex

Map index from source by calculating position based on slice values.

proxy pos = source pos - slice start / slice step

Source code in prettyqt\itemmodels\proxies\slicefilterproxymodel.py
def mapFromSource(self, source_index: core.ModelIndex) -> core.ModelIndex:
    """Map index from source by calculating position based on slice values.

    proxy pos = source pos - slice start / slice step
    """
    if self.sourceModel() is None or not source_index.isValid():
        return core.ModelIndex()
    row_pos = self.position_in_row_slice(source_index.row())
    col_pos = self.position_in_column_slice(source_index.column())
    return self.createIndex(row_pos, col_pos, source_index.internalPointer())

mapToSource(proxy_idx: core.ModelIndex) -> core.ModelIndex

Map index to source by calculating position from slice values.

source pos = slice start + proxy pos * slice step)

Source code in prettyqt\itemmodels\proxies\slicefilterproxymodel.py
def mapToSource(self, proxy_idx: core.ModelIndex) -> core.ModelIndex:
    """Map index to source by calculating position from slice values.

    source pos = slice start + proxy pos * slice step)
    """
    source = self.sourceModel()
    if source is None or not proxy_idx.isValid():
        return core.ModelIndex()
    col_range = self.get_column_range()
    row_range = self.get_row_range()
    col_pos = col_range.start + (col_range.step * proxy_idx.column())
    row_pos = row_range.start + (row_range.step * proxy_idx.row())
    return source.index(row_pos, col_pos)

Info

This is a slice proxy and can be selectively applied to a model. Read more about slices.

⌗ Property table

Qt Property Type Doc
objectName QString
sourceModel QAbstractItemModel
column_slice QVariantList Column slice to include for the proxy
row_slice QVariantList Row slice to include for the proxy