Skip to content

SliceChangeFlagsProxyModel

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
  1473290720848["itemmodels.SliceChangeFlagsProxyModel"]
  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 --> 1473290720848
  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 selectively change the ItemFlags of the source model.

table = widgets.TreeView()
data = dict(
    first=["John", "Mary"],
    last=["Doe", "Bo"],
    height=[5.5, 6.0],
    weight=[130, 150],
)
source_model = gui.StandardItemModel.from_dict(data)
table.set_model(source_model)
# table.proxifier.change_flags(enabled=False)
Image title

table = widgets.TreeView()
data = dict(
    first=["John", "Mary"],
    last=["Doe", "Bo"],
    height=[5.5, 6.0],
    weight=[130, 150],
)
source_model = gui.StandardItemModel.from_dict(data)
table.set_model(source_model)
table.proxifier.change_flags(enabled=False)
Image title

Supports changing all ItemRoles by passing keyword arguments.

Possible keyword arguments:

  • selectable: ItemFlag.ItemIsSelectable,
  • editable: ItemFlag.ItemIsEditable,
  • drag_enabled: ItemFlag.ItemIsDragEnabled,
  • drop_enabled: ItemFlag.ItemIsDropEnabled,
  • user_checkable: ItemFlag.ItemIsUserCheckable,
  • enabled: ItemFlag.ItemIsEnabled,
  • auto_tristate: ItemFlag.ItemIsAutoTristate,
  • never_has_children: ItemFlag.ItemNeverHasChildren,
  • user_tristate: ItemFlag.ItemIsUserTristate,

Example

model = MyModel()
table = widgets.TableView()
table.set_model(model)
table.proxifier[::2, 2:].change_flags(readable=False)
table.show()

or

indexer = (slice(None, None, 2), slice(2, None))
proxy = itemmodels.SliceChangeFlagsProxyModel(indexer=indexer, readable=False)
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt\itemmodels\proxies\slicechangeflagsproxymodel.py
class SliceChangeFlagsProxyModel(itemmodels.SliceIdentityProxyModel):
    """Proxy model to selectively change the ItemFlags of the source model.

    === "Without proxy"

        ```py
        table = widgets.TreeView()
        data = dict(
            first=["John", "Mary"],
            last=["Doe", "Bo"],
            height=[5.5, 6.0],
            weight=[130, 150],
        )
        source_model = gui.StandardItemModel.from_dict(data)
        table.set_model(source_model)
        # table.proxifier.change_flags(enabled=False)
        ```
        <figure markdown>
          ![Image title](../../images/slicechangeflagsproxymodel_before.png)
        </figure>

    === "With proxy"

        ```py
        table = widgets.TreeView()
        data = dict(
            first=["John", "Mary"],
            last=["Doe", "Bo"],
            height=[5.5, 6.0],
            weight=[130, 150],
        )
        source_model = gui.StandardItemModel.from_dict(data)
        table.set_model(source_model)
        table.proxifier.change_flags(enabled=False)
        ```
        <figure markdown>
          ![Image title](../../images/slicechangeflagsproxymodel_after.png)
        </figure>


    Supports changing all ItemRoles by passing keyword arguments.

    Possible keyword arguments:

    * `selectable`: ItemFlag.ItemIsSelectable,
    * `editable`: ItemFlag.ItemIsEditable,
    * `drag_enabled`: ItemFlag.ItemIsDragEnabled,
    * `drop_enabled`: ItemFlag.ItemIsDropEnabled,
    * `user_checkable`: ItemFlag.ItemIsUserCheckable,
    * `enabled`: ItemFlag.ItemIsEnabled,
    * `auto_tristate`: ItemFlag.ItemIsAutoTristate,
    * `never_has_children`: ItemFlag.ItemNeverHasChildren,
    * `user_tristate`: ItemFlag.ItemIsUserTristate,



    ### Example

    ```py
    model = MyModel()
    table = widgets.TableView()
    table.set_model(model)
    table.proxifier[::2, 2:].change_flags(readable=False)
    table.show()
    ```

    or

    ```py
    indexer = (slice(None, None, 2), slice(2, None))
    proxy = itemmodels.SliceChangeFlagsProxyModel(indexer=indexer, readable=False)
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "change_flags"
    ICON = "mdi.flag"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._flags_to_remove: constants.ItemFlag = constants.ItemFlag(0)
        self._flags_to_add: constants.ItemFlag = constants.ItemFlag(0)

    # def setData(
    #     self,
    #     index: core.ModelIndex,
    #     value: Any,
    #     role: constants.ItemDataRole = constants.EDIT_ROLE,
    # ) -> bool:
    #     if self.indexer.contains(index):
    #         logger.warning("Trying to set data on region covered by read-only proxy")
    #         return False
    #     return super().setData(index, value, role)

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        flags = super().flags(index)
        if self.indexer_contains(index):
            for flag in self._flags_to_remove:
                flags &= ~flag
            for flag in self._flags_to_add:
                flags |= flag
        return flags

    def set_flags_to_add(self, flags: constants.ItemFlag):
        with self.change_layout():
            self._flags_to_add = flags

    def get_flags_to_add(self) -> constants.ItemFlag:
        return self._flags_to_add

    def set_flags_to_remove(self, flags: constants.ItemFlag):
        with self.change_layout():
            self._flags_to_remove = flags

    def get_flags_to_remove(self) -> constants.ItemFlag:
        return self._flags_to_remove

    flags_to_add = core.Property(
        constants.ItemFlag,
        get_flags_to_add,
        set_flags_to_add,
        doc="Flags to set",
    )
    flags_to_remove = core.Property(
        constants.ItemFlag,
        get_flags_to_remove,
        set_flags_to_remove,
        doc="Flags to unset",
    )

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
flags_to_add PySide::PyObjectWrapper Flags to set
flags_to_remove PySide::PyObjectWrapper Flags to unset