Skip to content

ColumnOrderProxyModel

Qt Base Class: QIdentityProxyModel

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

Base classes

Name Children Inherits
IdentityProxyModel
prettyqt.core.identityproxymodel

⋔ Inheritance diagram

graph TD
  1473290739392["itemmodels.ColumnOrderProxyModel"]
  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"]
  1473299892128 --> 1473290739392
  1473299903840 --> 1473299892128
  1473299890176 --> 1473299903840
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289064768 --> 1473299892128
  1473289061840 --> 1473289064768
  1473289050128 --> 1473289061840
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: IdentityProxyModel

Proxy model which reorders the columns of the source model.

Proxy model which reorders / hides the columns of the source model by passing a list containing the new order. Order indexes can either be an integer or the Column header. If not all indexes are part of the list, then the missing sections will be hidden.

Example

table.proxifier.reorder_columns(order=[3, 2, 0])
table.show()
# or
model = MyModel()
proxy = ColumnOrderProxyModel(order=[3, 2, 0])
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt\itemmodels\proxies\columnorderproxymodel.py
class ColumnOrderProxyModel(core.IdentityProxyModel):
    """Proxy model which reorders the columns of the source model.

    Proxy model which reorders / hides the columns of the source model by passing a list
    containing the new order. Order indexes can either be an integer or the Column header.
    If not all indexes are part of the list, then the missing sections will be hidden.

    ### Example

    ```py
    table.proxifier.reorder_columns(order=[3, 2, 0])
    table.show()
    # or
    model = MyModel()
    proxy = ColumnOrderProxyModel(order=[3, 2, 0])
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "column_order"
    ICON = "mdi.reorder-vertical"

    def __init__(self, order: list[int | str], **kwargs):
        self._column_order = order
        super().__init__(**kwargs)
        self.set_column_order(order)

    def get_column_order(self) -> list[int]:
        return self._column_order

    def setSourceModel(self, model):
        super().setSourceModel(model)
        self._resolve_string_indexes()

    def _resolve_string_indexes(self):
        new_order = []
        source = self.sourceModel()
        for index in self._column_order:
            if isinstance(index, str):
                for i in range(source.columnCount()):
                    v = source.headerData(i, constants.HORIZONTAL, constants.DISPLAY_ROLE)
                    if v == index:
                        index = i
                        break
                else:
                    raise ValueError(index)
            new_order.append(index)
        self._column_order = new_order

    def set_column_order(self, order: list[int | str]):
        with self.reset_model():
            self._column_order = order
            if self.sourceModel() is not None:
                self._resolve_string_indexes()

    def mapToSource(self, proxy_index: core.ModelIndex) -> core.ModelIndex:
        if not proxy_index.isValid():
            return core.ModelIndex()
        return self.sourceModel().createIndex(
            proxy_index.row(),
            self._column_order[proxy_index.column()],
            proxy_index.internalPointer(),
        )

    def mapFromSource(self, source_index: core.ModelIndex) -> core.ModelIndex:
        if not source_index.isValid():
            return core.ModelIndex()
        proxy_column = self._column_order.index(source_index.column())
        return self.createIndex(
            source_index.row(), proxy_column, source_index.internalPointer()
        )

    def sibling(self, row: int, column: int, index: core.ModelIndex) -> core.ModelIndex:
        if column >= len(self._column_order):
            return core.ModelIndex()
        return self.index(row, column, index.parent())

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if orientation == constants.HORIZONTAL:
            section = self._column_order[section]
        return self.sourceModel().headerData(section, orientation, role)

    def hasChildren(self, parent_index: core.ModelIndex) -> bool:
        if self.sourceModel() is None or parent_index.column() > 0:
            return False
        source_parent = self.mapToSource(parent_index).sibling(parent_index.row(), 0)
        return self.sourceModel().rowCount(source_parent) > 0

    def parent(self, index: core.ModelIndex) -> core.ModelIndex:
        source_parent = self.mapToSource(index).parent()
        if not source_parent.isValid():
            return core.ModelIndex()
        return self.createIndex(source_parent.row(), 0, source_parent.internalPointer())

    def index(
        self, row: int, column: int, parent: core.ModelIndex | None = None
    ) -> core.ModelIndex:
        parent = parent or core.ModelIndex()
        if parent.column() > 0 or self.sourceModel() is None:
            return core.ModelIndex()
        source_parent = self.mapToSource(parent).sibling(parent.row(), 0)
        source_index = self.sourceModel().index(
            row, self._column_order[column], source_parent
        )
        if not source_index.isValid():
            return core.ModelIndex()
        return self.createIndex(row, column, source_index.internalPointer())

    def columnCount(self, index: core.ModelIndex | None = None) -> int:
        index = index or core.ModelIndex()
        return len(self._column_order)

    def rowCount(self, index: core.ModelIndex | None = None) -> int:
        index = index or core.ModelIndex()
        if self.sourceModel() is None or index.column() > 0:
            return 0
        source_parent = self.mapToSource(index).sibling(index.row(), 0)
        return self.sourceModel().rowCount(source_parent)

    order = core.Property(
        list,
        get_column_order,
        set_column_order,
        doc="Column order",
    )

⌗ Property table

Qt Property Type Doc
objectName QString
sourceModel QAbstractItemModel
order QVariantList Column order