Skip to content

SliceDisplayTextProxyModel

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
  1473290712064["itemmodels.SliceDisplayTextProxyModel"]
  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 --> 1473290712064
  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 convert and format non-str values for the DisplayRole.

Usually, formatting of numbers etc is done by the ItemDelegate. By moving the formatting into a proxy layer instead, we can keep the ItemDelegate spot free for other stuff.

Information about string formatting:

https://docs.python.org/3/library/string.html#format-specification-mini-language

Example

model = MyModel()
table = widgets.TableView()
table.set_model(model)
table[:, :3].proxify.format_text(int_format="{:0>2d}")
table.show()
# or
indexer = (slice(None), slice(None, 3))
proxy = SliceDisplayTextProxyModel(indexer=indexer, float_format="{:.4f}")
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt\itemmodels\proxies\slicedisplaytextproxymodel.py
class SliceDisplayTextProxyModel(itemmodels.SliceIdentityProxyModel):
    """Proxy model to convert and format non-str values for the DisplayRole.

    Usually, formatting of numbers etc is done by the ItemDelegate.
    By moving the formatting into a proxy layer instead, we can keep the ItemDelegate
    spot free for other stuff.

    Information about string formatting:

    https://docs.python.org/3/library/string.html#format-specification-mini-language

    ### Example

    ```py
    model = MyModel()
    table = widgets.TableView()
    table.set_model(model)
    table[:, :3].proxify.format_text(int_format="{:0>2d}")
    table.show()
    # or
    indexer = (slice(None), slice(None, 3))
    proxy = SliceDisplayTextProxyModel(indexer=indexer, float_format="{:.4f}")
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    def __init__(self, *args, **kwargs):
        self._int_format = "{:.4f}"
        self._float_format = "{:.4f}"
        self._datetime_format = "%m/%d/%Y, %H:%M:%S"
        self._date_format = "%m/%d/%Y"
        self._time_format = "%H:%M:%S"
        # self._force_override = False
        super().__init__(*args, **kwargs)

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not self.indexer_contains(index) or role != constants.DISPLAY_ROLE:
            return super().data(index, role)
        data = index.data()
        match data:
            case int():
                return self._int_format.format(data)
            case float():
                return self._float_format.format(data)
            case core.QDateTime():
                return data.toString(self._datetime_format)
            case datetime.datetime():
                return data.strftime(self._datetime_format)
            case core.QTime():
                return data.toString(self._time_format)
            case datetime.time():
                return data.strftime(self._time_format)
            case core.QDate():
                return data.toString(self._date_format)
            case datetime.date():
                return data.strftime(self._date_format)
            case _:
                return super().data(index, role)

    def set_int_format(self, fmt: str):
        self._int_format = fmt

    def get_int_format(self) -> str:
        return self._int_format

    def set_float_format(self, fmt: str):
        self._float_format = fmt

    def get_float_format(self) -> str:
        return self._float_format

    def set_datetime_format(self, fmt: str):
        self._datetime_format = fmt

    def get_datetime_format(self, fmt) -> str:
        return self._datetime_format

    def set_date_format(self, fmt: str):
        self._date_format = fmt

    def get_date_format(self, fmt) -> str:
        return self._date_format

    def set_time_format(self, fmt: str):
        self._time_format = fmt

    def get_time_format(self, fmt) -> str:
        return self._time_format

    int_format = core.Property(
        str,
        get_int_format,
        set_int_format,
        doc="String format for integers",
    )
    float_format = core.Property(
        str,
        get_float_format,
        set_float_format,
        doc="String format for floats",
    )
    datetime_format = core.Property(
        str,
        get_datetime_format,
        set_datetime_format,
        doc="String format for datetime objects",
    )
    date_format = core.Property(
        str,
        get_date_format,
        set_date_format,
        doc="String format for date objects",
    )
    time_format = core.Property(
        str,
        get_time_format,
        set_time_format,
        doc="String format for time objects",
    )

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
int_format QString String format for integers
float_format QString String format for floats
datetime_format QString String format for datetime objects
date_format QString String format for date objects
time_format QString String format for time objects