Skip to content

ChangeHeadersProxyModel

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
  1473290754032["itemmodels.ChangeHeadersProxyModel"]
  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 --> 1473290754032
  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 for changing the header data (either horizontal or vertical).

Header data can either be changed by passing a list with same length as source length or by passing a dictionary with index as key and new value as value (Example: {1: "abc", 3: "def"} changes section 1 to "abc" and section 3 to "def") Apart from the regular use case of changing the text, the other roles can be changed, too.

Example

table.proxifier.change_headers(header=["x", "y", "z"],
    orientation=constants.HORIZONTAL,
    role=constants.DISPLAY_ROLE
)
table.show()
# or
model = MyModel()
proxy = ChangeHeadersProxyModel(
    header=["x", "y", "z"], orientation=constants.VERTICAL
)
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt\itemmodels\proxies\changeheadersproxymodel.py
class ChangeHeadersProxyModel(core.IdentityProxyModel):
    """Proxy model for changing the header data (either horizontal or vertical).

    Header data can either be changed by passing a list with same length as source length
    or by passing a dictionary with index as key and new value as value
    (Example: {1: "abc", 3: "def"} changes section 1 to "abc" and section 3 to "def")
    Apart from the regular use case of changing the text, the other roles can be changed,
    too.

    ### Example

    ```py
    table.proxifier.change_headers(header=["x", "y", "z"],
        orientation=constants.HORIZONTAL,
        role=constants.DISPLAY_ROLE
    )
    table.show()
    # or
    model = MyModel()
    proxy = ChangeHeadersProxyModel(
        header=["x", "y", "z"], orientation=constants.VERTICAL
    )
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "change_headers"
    ICON = "table-headers-eye"

    def __init__(
        self,
        header: list[Any] | dict[int, Any],
        orientation: constants.Orientation
        | constants.OrientationStr = constants.HORIZONTAL,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self._orientation = constants.ORIENTATION.get_enum_value(orientation)
        self._header = header
        self._role = role

    def setSourceModel(self, model):
        header_len = (
            model.columnCount()
            if self._orientation == constants.HORIZONTAL
            else model.rowCount()
        )
        if isinstance(self._header, list) and len(self._header) != header_len:
            raise ValueError("list needs to be same list as header")
        super().setSourceModel(model)

    def get_header(self) -> list[int]:
        return self._header

    def set_header(
        self,
        header: list[str] | dict[int, str],
        orientation: constants.Orientation | constants.OrientationStr,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        with self.reset_model():
            self._header = header
            self._orientation = constants.ORIENTATION.get_enum_value(orientation)
            self._role = role

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if orientation == self._orientation and role == self._role:
            if isinstance(self._header, dict) and section in self._header:
                return self._header[section]
            elif isinstance(self._header, list):
                return self._header[section]
        return self.sourceModel().headerData(section, orientation, role)

    header = core.Property(
        object,
        get_header,
        set_header,
        doc="New headers (dict / list)",
    )

⌗ Property table

Qt Property Type Doc
objectName QString
sourceModel QAbstractItemModel
header PySide::PyObjectWrapper New headers (dict / list)