QItemSelectionModel Class
The QItemSelectionModel class keeps track of a view's selected items. More…
| Header | #include <QItemSelectionModel> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Types
| enum | SelectionFlag |
| typedef | SelectionFlags |
Public Functions
| QItemSelectionModel(QAbstractItemModel *model = nullptr) | |
| QItemSelectionModel(QAbstractItemModel *model, QObject *parent) | |
| ~QItemSelectionModel() | |
| QModelIndex | currentIndex() const |
| bool | isSelected(const QModelIndex &index) const |
| bool | isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const |
| bool | isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const |
| bool | rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const |
| bool | columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const |
| bool | hasSelection() const |
| QModelIndexList | selectedIndexes() const |
| QModelIndexList | selectedRows(int column = 0) const |
| QModelIndexList | selectedColumns(int row = 0) const |
| const QItemSelection | selection() const |
| QAbstractItemModel * | model() |
| const QAbstractItemModel * | model() const |
| void | setModel(QAbstractItemModel *model) |
Public Slots
| virtual void | setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) |
| virtual void | select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) |
| virtual void | select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) |
| virtual void | clear() |
| virtual void | reset() |
| void | clearSelection() |
| virtual void | clearCurrentIndex() |
Signals
| void | selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) |
| void | currentChanged(const QModelIndex ¤t, const QModelIndex &previous) |
| void | currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) |
| void | currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous) |
| void | modelChanged(QAbstractItemModel *model) |
Protected Functions
| void | emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection) |
Detailed Description
A selection model holds the selection and the current item of a model, as used by views. select() changes the selection with a combination of SelectionFlag values, and setCurrentIndex() moves the current item.
QItemSelectionModel selection(&model);
selection.select(model.index(0, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
selection.isRowSelected(0); // true
See also QItemSelection and QAbstractItemModel.
Differences from Qt
Selections are kept as cells and turned back into ranges, which is simple but slower than Qt for very large selections. The selection follows the model through persistent indexes; layout changes keep the selected items selected.
Member Type Documentation
enum QItemSelectionModel::SelectionFlag
| Constant | Description |
|---|---|
QItemSelectionModel::NoUpdate | The selection does not change. |
QItemSelectionModel::Clear | The whole selection is cleared first. |
QItemSelectionModel::Select | The items are selected. |
QItemSelectionModel::Deselect | The items are deselected. |
QItemSelectionModel::Toggle | The items are selected or deselected depending on their state. |
QItemSelectionModel::Current | The current selection is updated (as with a rubber band) instead of being added to the committed selection. |
QItemSelectionModel::Rows | Whole rows are affected. |
QItemSelectionModel::Columns | Whole columns are affected. |
QItemSelectionModel::SelectCurrent | Select | Current. |
QItemSelectionModel::ToggleCurrent | Toggle | Current. |
QItemSelectionModel::ClearAndSelect | Clear | Select. |
typedef QItemSelectionModel::SelectionFlags
The SelectionFlags type is a typedef for QFlags<SelectionFlag>. It stores an OR combination of SelectionFlag values.
Member Function Documentation
QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model = nullptr)
Constructs a selection model for model.
QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent)
Constructs a selection model for model with the given parent.
QItemSelectionModel::~QItemSelectionModel()
Destroys the selection model.
void QItemSelectionModel::clear()
Clears the selection and the current item, emitting the signals.
void QItemSelectionModel::clearCurrentIndex()
Clears the current item and emits currentChanged().
This function was introduced in 4.4.
void QItemSelectionModel::clearSelection()
Clears the selection and emits selectionChanged().
This function was introduced in 5.0.
bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const
Returns true if any item of column under parent is selected; otherwise returns false.
void QItemSelectionModel::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
This signal is emitted when the current item changes from previous to current.
void QItemSelectionModel::currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous)
This signal is emitted when the current item moves to another column, with the new current and the previous item.
QModelIndex QItemSelectionModel::currentIndex() const
Returns the current item, or an invalid index.
See also setCurrentIndex().
void QItemSelectionModel::currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
This signal is emitted when the current item moves to another row, with the new current and the previous item.
void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection)
Emits selectionChanged() with the difference between newSelection and oldSelection, unless they hold the same items.
bool QItemSelectionModel::hasSelection() const
Returns true if any item is selected; otherwise returns false.
bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const
Returns true if all selectable items of column under parent are selected; otherwise returns false.
bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const
Returns true if all selectable items of row under parent are selected; otherwise returns false.
bool QItemSelectionModel::isSelected(const QModelIndex &index) const
Returns true if index is selected; otherwise returns false.
QAbstractItemModel * QItemSelectionModel::model()
Returns the model.
const QAbstractItemModel * QItemSelectionModel::model() const
This is an overloaded function.
Returns the model.
void QItemSelectionModel::modelChanged(QAbstractItemModel *model)
This signal is emitted when the model changes to model.
This function was introduced in 5.5.
void QItemSelectionModel::reset()
Clears the selection and the current item without emitting signals.
bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const
Returns true if any item of row under parent is selected; otherwise returns false.
void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Changes the selection of the item index with command.
void QItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
Changes the selection with selection and command: Clear empties it first, Select, Deselect and Toggle change the items of selection, and Rows and Columns extend them to whole rows or columns. With Current, the change replaces the previous current change instead of being committed. Emits selectionChanged() if the selection changed.
QModelIndexList QItemSelectionModel::selectedColumns(int row = 0) const
Returns the items in row of the columns whose selectable items are all selected.
QModelIndexList QItemSelectionModel::selectedIndexes() const
Returns the selected items.
QModelIndexList QItemSelectionModel::selectedRows(int column = 0) const
Returns the items in column of the rows whose selectable items are all selected.
const QItemSelection QItemSelectionModel::selection() const
Returns the selection: the committed ranges combined with the current selection.
void QItemSelectionModel::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
This signal is emitted when the selection changes: selected holds the items that became selected and deselected those that stopped being selected.
void QItemSelectionModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Makes index the current item and changes the selection with command, then emits currentChanged() and, as appropriate, currentRowChanged() and currentColumnChanged().
void QItemSelectionModel::setModel(QAbstractItemModel *model)
Sets the model to model, clearing the selection and the current item without emitting signals, and emits modelChanged().
This function was introduced in 5.5.
See also model().