QItemSelectionModel Class
QItemSelectionModel 类用于跟踪视图中已选中的项目。更多内容...
| 头文件: | #include <QItemSelectionModel> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 继承自: | QObject |
公共类型
| enum | SelectionFlag { NoUpdate, Clear, Select, Deselect, Toggle, …, ClearAndSelect } |
| flags | SelectionFlags |
属性
- selectedIndexes : QModelIndexList
公共函数
| QItemSelectionModel(QAbstractItemModel *model = nullptr) | |
| QItemSelectionModel(QAbstractItemModel *model, QObject *parent) | |
| virtual | ~QItemSelectionModel() |
| 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) |
公共插槽
| 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) |
信号
| void | currentChanged(const QModelIndex ¤t, const QModelIndex &previous) |
| void | currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous) |
| void | currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) |
| void | modelChanged(QAbstractItemModel *model) |
| void | selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) |
受保护函数
| void | emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection) |
详细说明
QItemSelectionModel 负责跟踪视图中(或针对同一模型的多个视图中)所选的项目。它还负责跟踪视图中当前选中的项目。
QItemSelectionModel 类是模型/视图类之一,属于 Qt的模型/视图框架。
选中的项目通过范围进行存储。每当您想要修改选中的项目时,请使用select(),并提供QItemSelection ,或者同时提供QModelIndex 和QItemSelectionModel::SelectionFlag 。
QItemSelectionModel 采用两层架构来管理选择,既处理已提交的选中项目,也处理当前选择范围内的项目。当前选中的项目属于当前交互式选择范围(例如橡皮筋选择或按住 Shift 键进行的选择)。
要更新当前选中的项目,请使用QItemSelectionModel::Current 与其他任何SelectionFlags 的按位或运算。如果省略QItemSelectionModel::Current 命令,将创建一个新的当前选择,并将之前的当前选择添加到整体选择中。所有函数都作用于这两个层;例如,selecteditems()将返回来自这两个层的项目。
注意:自 5.5版本起 ,model 、hasSelection 和currentIndex 均为元对象属性。
另请参阅 “模型/视图编程”和QAbstractItemModel 。
成员类型文档
enum QItemSelectionModel::SelectionFlag
flags QItemSelectionModel::SelectionFlags
此枚举描述了选择模型的更新方式。
| 常量 | 值 | 描述 |
|---|---|---|
QItemSelectionModel::NoUpdate | 0x0000 | 将不进行任何选择。 |
QItemSelectionModel::Clear | 0x0001 | 将清除所有选择。 |
QItemSelectionModel::Select | 0x0002 | 将选中所有指定的索引。 |
QItemSelectionModel::Deselect | 0x0004 | 将取消选中所有指定的索引。 |
QItemSelectionModel::Toggle | 0x0008 | 将根据当前状态,选中或取消选中所有指定的索引。 |
QItemSelectionModel::Current | 0x0010 | 将更新当前选择。 |
QItemSelectionModel::Rows | 0x0020 | 所有索引都将扩展为跨行索引。 |
QItemSelectionModel::Columns | 0x0040 | 所有索引都将扩展为跨列。 |
QItemSelectionModel::SelectCurrent | Select | Current | 为方便起见,提供了“选择”和“当前”的组合选项。 |
QItemSelectionModel::ToggleCurrent | Toggle | Current | 为方便起见,提供了“切换”和“当前”的组合。 |
QItemSelectionModel::ClearAndSelect | Clear | Select | 为方便起见,提供了“清除”和“选择”的组合。 |
SelectionFlags 类型是QFlags<SelectionFlag> 的 typedef。它存储 SelectionFlag 值的“或”组合。
属性文档
[read-only] selectedIndexes : QModelIndexList
访问功能:
| QModelIndexList | selectedIndexes() const |
通知信号:
| void | selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) |
成员函数文档
[explicit] QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model = nullptr)
构建一个基于指定项目model 运行的筛选模型。
[explicit] QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent)
构建一个基于指定项model 并使用parent 进行操作的选择模型。
[virtual noexcept] QItemSelectionModel::~QItemSelectionModel()
删除所选模型。
[virtual slot] void QItemSelectionModel::clear()
清除选择模型。触发selectionChanged()和currentChanged()事件。
[virtual slot] void QItemSelectionModel::clearCurrentIndex()
清除当前索引。发出currentChanged()事件。
[slot] void QItemSelectionModel::clearSelection()
清除选择模型中的选择。触发selectionChanged()事件。
[invokable] bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const
如果column 中选中了任何具有给定parent 的项,则返回true 。
注意:自 Qt 5.15起 ,parent 的默认参数为空模型索引。
注意: 可通过元对象系统和 QML 调用此 函数。请参阅Q_INVOKABLE 。
[signal] void QItemSelectionModel::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
每当当前项目发生变化时,都会触发此信号。此时,previous 模型中的项目索引将被current 中的索引替换,作为选区中的当前项目。
请注意,当项目模型被重置时,不会发出此信号。
另请参阅 currentIndex()、setCurrentIndex() 和selectionChanged()。
[signal] void QItemSelectionModel::currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous)
当current 项发生变化,且其列与previous 当前项的列不同时,将触发此信号。
请注意,当项目模型被重置时,不会触发此信号。
另请参阅 currentChanged()、currentRowChanged()、currentIndex() 以及setCurrentIndex()。
QModelIndex QItemSelectionModel::currentIndex() const
返回当前项目的模型项索引;如果当前没有项目,则返回一个无效索引。
另请参阅 setCurrentIndex()。
[signal] void QItemSelectionModel::currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
当current 项发生变化,且其行与previous 当前项的行不同时,会触发此信号。
请注意,当项目模型被重置时,不会触发此信号。
另请参阅 currentChanged()、currentColumnChanged()、currentIndex() 和setCurrentIndex()。
[protected] void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection)
比较两个选择集newSelection 和oldSelection ,并返回包含未选中项和已选中项的selectionChanged()。
bool QItemSelectionModel::hasSelection() const
如果选择模型中包含任何已选项目,则返回true ;否则返回false 。
[invokable] bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const
如果column 中所有项目均被选中(且符合给定的parent ),则返回true 。
请注意,此函数通常比对同一列中的所有项目分别调用isSelected() 更快,且不可选项目将被忽略。
注意:自 Qt 5.15 起,parent 的默认参数是一个空模型索引。
注意: 可以通过元对象系统和 QML 调用此 函数。参见Q_INVOKABLE 。
[invokable] bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const
如果row 中所有项目均被选中(且满足给定的parent 条件),则返回true 。
请注意,此函数通常比对同一行中的所有项目分别调用isSelected() 更快,且不可选项目将被忽略。
注意:自 Qt 5.15起 ,parent 的默认参数为空模型索引。
注意: 可以通过元对象系统和从 QML 调用此 函数。参见Q_INVOKABLE 。
[invokable] bool QItemSelectionModel::isSelected(const QModelIndex &index) const
如果给定的模型项index 被选中,则返回true 。
注意: 可通过元对象系统和 QML 调用此 函数。参见Q_INVOKABLE 。
QAbstractItemModel *QItemSelectionModel::model()
返回被选择模型操作的项模型。
另请参阅 setModel()。
const QAbstractItemModel *QItemSelectionModel::model() const
返回由选择模型操作的项模型。
[signal] void QItemSelectionModel::modelChanged(QAbstractItemModel *model)
当使用setModel() 成功设置model 时,会发出此信号。
[virtual slot] void QItemSelectionModel::reset()
清除选择模型。不会发出任何信号。
[invokable] bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const
如果row 中存在任何符合给定parent 的已选项,则返回true 。
注意:自 Qt 5.15起 ,parent 的默认参数为空模型索引。
注意: 可通过元对象系统以及从 QML 中调用此 函数。参见Q_INVOKABLE 。
[virtual slot] void QItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
使用指定的command 选择项selection ,并触发selectionChanged()。
注意:此 插槽是重载的。要连接到此插槽:
// 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); }); 另请参阅 QItemSelectionModel::SelectionFlag 。
[virtual slot] void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
使用指定的command 选择模型项index ,并发出selectionChanged()。
注意:此 插槽被重载。要连接到此插槽:
// 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); }); 另请参阅 QItemSelectionModel::SelectionFlags 。
[invokable] QModelIndexList QItemSelectionModel::selectedColumns(int row = 0) const
返回给定row 中所有行均被选中的列的索引。
注意: 可通过元对象系统或从 QML 中调用此 函数。参见Q_INVOKABLE 。
另请参阅 selectedIndexes() 和selectedRows()。
QModelIndexList QItemSelectionModel::selectedIndexes() const
返回所有已选模型项索引的列表。该列表不包含重复项,且未按顺序排序。
注意:这是 selectedIndexes 属性的获取函数 。
[invokable] QModelIndexList QItemSelectionModel::selectedRows(int column = 0) const
返回给定column 中所有列均被选中的行对应的索引。
注意: 可通过元对象系统或从 QML 中调用此 函数。参见Q_INVOKABLE 。
另请参阅 selectedIndexes() 和selectedColumns()。
const QItemSelection QItemSelectionModel::selection() const
返回存储在选择模型中的选择范围。
[signal] void QItemSelectionModel::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
每当选择发生变化时,都会触发此信号。选择的变化通过deselected 项的项选择和selected 项的项选择来表示。
请注意,当前索引的变化与选中状态的变化是相互独立的。另请注意,当项目模型被重置时,此信号不会被触发。
那些仍被选中但索引发生变化的项目不会包含在selected 和deselected 中。因此,如果仅选中项目的索引发生变化,则即使selected 和deselected 均为空,该信号仍可能被触发。
注意:不允许 从直接连接到此信号的插槽内部修改模型(例如,通过调用 setData())。当模型正在被修改时(例如在删除行或列,或重置模型期间),此信号可能会被触发。 在此类情况下尝试进行额外更改可能会导致未定义的行为。特别是,嵌套修改可能会破坏内部状态,例如由QSortFilterProxyModel 维护的映射结构。
注: 这是属性selectedIndexes的通知 信号。
另请参阅 select() 和currentChanged()。
[virtual slot] void QItemSelectionModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
将模型项index 设为当前项,并触发currentChanged()事件。当前项用于键盘导航和焦点指示;它与任何选中项无关,尽管选中项也可以是当前项。
根据指定的command ,index 也可以成为当前选择的一部分。
另请参阅 currentIndex() 和select()。
void QItemSelectionModel::setModel(QAbstractItemModel *model)
将模型设置为model 。此时将触发modelChanged()信号。
另请参阅 model() 和modelChanged()。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.