QItemSelectionModel Class | Qt Core | Qt 6.11.1
Description: The QItemSelectionModel class keeps track of a view's selected items.

- Qt 6.11
- Qt Core
- C++ Classes
- QItemSelectionModel
- Qt 6.11.1 Reference Documentation

# QItemSelectionModel Class

The QItemSelectionModel class keeps track of a view's selected items. More...

Header: | `#include <QItemSelectionModel>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`
Inherits: | QObject

- List of all members, including inherited members

## Public Types

enum | SelectionFlag { NoUpdate, Clear, Select, Deselect, Toggle, …, ClearAndSelect }
flags | SelectionFlags

## Properties

- selectedIndexes : QModelIndexList

## Public Functions

| QItemSelectionModel(QAbstractItemModel *model = nullptr)
| QItemSelectionModel(QAbstractItemModel *model, QObject *parent)
virtual | ~QItemSelectionModel()
QBindable<QAbstractItemModel *> | bindableModel()
bool | columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const
QModelIndex | currentIndex() const
bool | hasSelection() const
bool | isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const
bool | isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const
bool | isSelected(const QModelIndex &index) const
QAbstractItemModel * | model()
const QAbstractItemModel * | model() const
bool | rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const
QModelIndexList | selectedColumns(int row = 0) const
QModelIndexList | selectedIndexes() const
QModelIndexList | selectedRows(int column = 0) const
const QItemSelection | selection() const
void | setModel(QAbstractItemModel *model)

## Public Slots

virtual void | clear()
virtual void | clearCurrentIndex()
void | clearSelection()
virtual void | reset()
virtual void | select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
virtual void | select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
virtual void | setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)

## Signals

void | currentChanged(const QModelIndex &current, const QModelIndex &previous)
void | currentColumnChanged(const QModelIndex &current, const QModelIndex &previous)
void | currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
void | modelChanged(QAbstractItemModel *model)
void | selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)

## Protected Functions

void | emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection)

## Detailed Description

A QItemSelectionModel keeps track of the selected items in a view, or in several views onto the same model. It also keeps track of the currently selected item in a view.

The QItemSelectionModel class is one of the Model/View Classes and is part of Qt's model/view framework.

The selected items are stored using ranges. Whenever you want to modify the selected items use select() and provide either a QItemSelection, or a QModelIndex and a QItemSelectionModel::SelectionFlag.

The QItemSelectionModel takes a two layer approach to selection management, dealing with both selected items that have been committed and items that are part of the current selection. The current selected items are part of the current interactive selection (for example with rubber-band selection or keyboard-shift selections).

To update the currently selected items, use the bitwise OR of QItemSelectionModel::Current and any of the other SelectionFlags. If you omit the QItemSelectionModel::Current command, a new current selection will be created, and the previous one added to the whole selection. All functions operate on both layers; for example, selecteditems() will return items from both layers.

Note: Since 5.5, model, hasSelection, and currentIndex are meta-object properties.

See also Model/View Programming and QAbstractItemModel.

## Member Type Documentation

### enum QItemSelectionModel::SelectionFlag
flags QItemSelectionModel::SelectionFlags

This enum describes the way the selection model will be updated.

Constant | Value | Description
--- | --- | ---
`QItemSelectionModel::NoUpdate` | `0x0000` | No selection will be made.
`QItemSelectionModel::Clear` | `0x0001` | The complete selection will be cleared.
`QItemSelectionModel::Select` | `0x0002` | All specified indexes will be selected.
`QItemSelectionModel::Deselect` | `0x0004` | All specified indexes will be deselected.
`QItemSelectionModel::Toggle` | `0x0008` | All specified indexes will be selected or deselected depending on their current state.
`QItemSelectionModel::Current` | `0x0010` | The current selection will be updated.
`QItemSelectionModel::Rows` | `0x0020` | All indexes will be expanded to span rows.
`QItemSelectionModel::Columns` | `0x0040` | All indexes will be expanded to span columns.
`QItemSelectionModel::SelectCurrent` | `Select | Current` | A combination of Select and Current, provided for convenience.
`QItemSelectionModel::ToggleCurrent` | `Toggle | Current` | A combination of Toggle and Current, provided for convenience.
`QItemSelectionModel::ClearAndSelect` | `Clear | Select` | A combination of Clear and Select, provided for convenience.

The SelectionFlags type is a typedef for QFlags<SelectionFlag>. It stores an OR combination of SelectionFlag values.

## Property Documentation

### `[read-only]` selectedIndexes : QModelIndexList

Access functions:

QModelIndexList | selectedIndexes() const

Notifier signal:

void | selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)

## Member Function Documentation

### `[explicit]` QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model = nullptr)

Constructs a selection model that operates on the specified item model.

### `[explicit]` QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent)

Constructs a selection model that operates on the specified item model with parent.

### `[virtual noexcept]` QItemSelectionModel::~QItemSelectionModel()

Destroys the selection model.

### `[virtual slot]` void QItemSelectionModel::clear()

Clears the selection model. Emits selectionChanged() and currentChanged().

### `[virtual slot]` void QItemSelectionModel::clearCurrentIndex()

Clears the current index. Emits currentChanged().

### `[slot]` void QItemSelectionModel::clearSelection()

Clears the selection in the selection model. Emits selectionChanged().

### `[invokable]` bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const

Returns `true` if there are any items selected in the column with the given parent.

Note: Since Qt 5.15, the default argument for parent is an empty model index.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[signal]` void QItemSelectionModel::currentChanged(const QModelIndex &current, const QModelIndex &previous)

This signal is emitted whenever the current item changes. The previous model item index is replaced by the current index as the selection's current item.

Note that this signal will not be emitted when the item model is reset.

See also currentIndex(), setCurrentIndex(), and selectionChanged().

### `[signal]` void QItemSelectionModel::currentColumnChanged(const QModelIndex &current, const QModelIndex &previous)

This signal is emitted if the current item changes and its column is different to the column of the previous current item.

Note that this signal will not be emitted when the item model is reset.

See also currentChanged(), currentRowChanged(), currentIndex(), and setCurrentIndex().

### QModelIndex QItemSelectionModel::currentIndex() const

Returns the model item index for the current item, or an invalid index if there is no current item.

See also setCurrentIndex().

### `[signal]` void QItemSelectionModel::currentRowChanged(const QModelIndex &current, const QModelIndex &previous)

This signal is emitted if the current item changes and its row is different to the row of the previous current item.

Note that this signal will not be emitted when the item model is reset.

See also currentChanged(), currentColumnChanged(), currentIndex(), and setCurrentIndex().

### `[protected]` void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection)

Compares the two selections newSelection and oldSelection and emits selectionChanged() with the deselected and selected items.

### bool QItemSelectionModel::hasSelection() const

Returns `true` if the selection model contains any selected item, otherwise returns `false`.

### `[invokable]` bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const

Returns `true` if all items are selected in the column with the given parent.

Note that this function is usually faster than calling isSelected() on all items in the same column and that unselectable items are ignored.

Note: Since Qt 5.15, the default argument for parent is an empty model index.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const

Returns `true` if all items are selected in the row with the given parent.

Note that this function is usually faster than calling isSelected() on all items in the same row and that unselectable items are ignored.

Note: Since Qt 5.15, the default argument for parent is an empty model index.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` bool QItemSelectionModel::isSelected(const QModelIndex &index) const

Returns `true` if the given model item index is selected.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### QAbstractItemModel *QItemSelectionModel::model()

Returns the item model operated on by the selection model.

See also setModel().

### const QAbstractItemModel *QItemSelectionModel::model() const

Returns the item model operated on by the selection model.

### `[signal]` void QItemSelectionModel::modelChanged(QAbstractItemModel *model)

This signal is emitted when the model is successfully set with setModel().

See also model() and setModel().

### `[virtual slot]` void QItemSelectionModel::reset()

Clears the selection model. Does not emit any signals.

### `[invokable]` bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const

Returns `true` if there are any items selected in the row with the given parent.

Note: Since Qt 5.15, the default argument for parent is an empty model index.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[virtual slot]` void QItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)

Selects the item selection using the specified command, and emits selectionChanged().

Note: This slot is overloaded. To connect to this slot:

     // Connect using qOverload:
     connect(sender, &SenderClass::signal,
             itemSelectionModel, qOverload(&QItemSelectionModel::select));

     // Or using a lambda as wrapper:
     connect(sender, &SenderClass::signal,
             itemSelectionModel, [receiver = itemSelectionModel](const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) { receiver->select(selection, command); });

For more examples and approaches, see connecting to overloaded slots.

See also QItemSelectionModel::SelectionFlag.

### `[virtual slot]` void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)

Selects the model item index using the specified command, and emits selectionChanged().

Note: This slot is overloaded. To connect to this slot:

     // Connect using qOverload:
     connect(sender, &SenderClass::signal,
             itemSelectionModel, qOverload(&QItemSelectionModel::select));

     // Or using a lambda as wrapper:
     connect(sender, &SenderClass::signal,
             itemSelectionModel, [receiver = itemSelectionModel](const QModelIndex &index, QItemSelectionModel::SelectionFlags command) { receiver->select(index, command); });

For more examples and approaches, see connecting to overloaded slots.

See also QItemSelectionModel::SelectionFlags.

### `[invokable]` QModelIndexList QItemSelectionModel::selectedColumns(int row = 0) const

Returns the indexes in the given row for columns where all rows are selected.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

See also selectedIndexes() and selectedRows().

### QModelIndexList QItemSelectionModel::selectedIndexes() const

Returns a list of all selected model item indexes. The list contains no duplicates, and is not sorted.

Note: Getter function for property selectedIndexes.

### `[invokable]` QModelIndexList QItemSelectionModel::selectedRows(int column = 0) const

Returns the indexes in the given column for the rows where all columns are selected.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

See also selectedIndexes() and selectedColumns().

### const QItemSelection QItemSelectionModel::selection() const

Returns the selection ranges stored in the selection model.

### `[signal]` void QItemSelectionModel::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)

This signal is emitted whenever the selection changes. The change in the selection is represented as an item selection of deselected items and an item selection of selected items.

Note the that the current index changes independently from the selection. Also note that this signal will not be emitted when the item model is reset.

Items which stay selected but change their index are not included in selected and deselected. Thus, this signal might be emitted with both selected and deselected empty, if only the indices of selected items change.

Note: It is not permitted to modify the model (e.g., by calling setData()) from within a slot connected directly to this signal. This signal may be emitted while the model is in the process of being modified, for example during row or column removal, or a model reset. Attempting to perform additional changes at such times can lead to undefined behavior. In particular, nested modifications can corrupt internal state, such as the mapping structures maintained by QSortFilterProxyModel.

Note: Notifier signal for property selectedIndexes.

See also select() and currentChanged().

### `[virtual slot]` void QItemSelectionModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)

Sets the model item index to be the current item, and emits currentChanged(). The current item is used for keyboard navigation and focus indication; it is independent of any selected items, although a selected item can also be the current item.

Depending on the specified command, the index can also become part of the current selection.

See also currentIndex() and select().

### void QItemSelectionModel::setModel(QAbstractItemModel *model)

Sets the model to model. The modelChanged() signal will be emitted.

See also model() and modelChanged().