Skip to content

LinkedSelectionModel

Qt Base Class: QItemSelectionModel

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

Base classes

Name Children Inherits
ItemSelectionModel
prettyqt.core.itemselectionmodel

⋔ Inheritance diagram

graph TD
  1473290763792["itemmodels.LinkedSelectionModel"]
  1473299851136["core.ItemSelectionModel"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473288881280["QtCore.QItemSelectionModel"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473299851136 --> 1473290763792
  1473299815024 --> 1473299851136
  140713234304496 --> 1473299815024
  1473288881280 --> 1473299851136
  1473288842240 --> 1473288881280
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: ItemSelectionModel

An ItemSelectionModel which link selections between item models.

Source code in prettyqt\itemmodels\proxies\linkedselectionmodel.py
class LinkedSelectionModel(core.ItemSelectionModel):
    """An ItemSelectionModel which link selections between item models."""

    # TODO: atm this doesnt need to inherit from ItemSelectionModel.
    # Not sure if there is any advantage in doing so...
    # Otherwise we could rename to SelectionLinker and just inherit from
    # object / core.Object
    def __init__(
        self,
        *itemviews,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self._itemviews = itemviews
        self._models = [w.model() for w in itemviews]
        self._mapper = itemmodels.ProxyMapper(*self._models)
        for w in itemviews:
            w.selectionModel().currentChanged.connect(self._on_current_change)
            w.selectionModel().selectionChanged.connect(self._on_selection_change)

    def _on_current_change(self, new: core.ModelIndex, _):
        source = self.sender().model()
        source_index = self._models.index(source)
        target_indexes = list(range(len(self._models)))
        target_indexes.remove(source_index)
        for target_index in target_indexes:
            mapped = self._mapper.map_index(
                source=source_index, target=target_index, index=new
            )
            self._itemviews[target_index].setCurrentIndex(mapped)

    def _on_selection_change(self, new: core.QItemSelection, _):
        source_model = self.sender().model()
        source_index = self._models.index(source_model)
        target_indexes = list(range(len(self._models)))
        target_indexes.remove(source_index)
        for target_index in target_indexes:
            selected = self._mapper.map_selection(
                source=source_index, target=target_index, selection=new
            )
            sel_model = self._itemviews[target_index].selectionModel()
            sel_model.select(selected, sel_model.SelectionFlag.Select)

⌗ Property table

Qt Property Type Doc
objectName QString
model QAbstractItemModel
hasSelection bool
currentIndex QModelIndex
selection QItemSelection
selectedIndexes QList