QItemSelectionModel Class

QItemSelectionModel 类用于跟踪视图的选定项。更多

Header: #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

属性

公共功能

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 &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)

受保护功能

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

详细说明

QItemSelectionModel 可跟踪一个视图或同一模型中多个视图的选定项。它还能跟踪视图中当前选中的项目。

QItemSelectionModel 类是模型/视图类之一,也是 Qt模型/视图框架的一部分。

被选中的项目使用范围存储。无论何时您想修改选中的项目,请使用select() 并提供QItemSelectionQModelIndexQItemSelectionModel::SelectionFlag

QItemSelectionModel 采用双层方法进行选择管理,既处理已提交的选定项,也处理属于当前选择的项目。当前选中项是当前交互式选区的一部分(例如橡皮筋选区或键盘移位选区)。

要更新当前选中项,可使用QItemSelectionModel::Current 和其他任何SelectionFlags 的 bitwise OR。如果省略QItemSelectionModel::Current 命令,则会创建一个新的当前选区,并将之前的选区添加到整个选区中。所有函数都对两层进行操作;例如,selecteditems() 将返回两层的项目。

注: 自 5.5 起,modelhasSelectioncurrentIndex 都是元对象属性。

另请参阅 模型/视图编程QAbstractItemModel

成员类型文档

枚举 QItemSelectionModel::SelectionFlag
flags QItemSelectionModel::SelectionFlags

该枚举描述了选择模型的更新方式。

常量说明
QItemSelectionModel::NoUpdate0x0000不进行选择。
QItemSelectionModel::Clear0x0001将清除全部选择。
QItemSelectionModel::Select0x0002将选择所有指定的索引。
QItemSelectionModel::Deselect0x0004取消选择所有指定索引。
QItemSelectionModel::Toggle0x0008根据当前状态选择或取消选择所有指定的索引。
QItemSelectionModel::Current0x0010当前选择将被更新。
QItemSelectionModel::Rows0x0020所有索引都将扩展到跨行。
QItemSelectionModel::Columns0x0040所有索引都将扩展到跨列。
QItemSelectionModel::SelectCurrentSelect | Current选择 "和 "当前 "的组合,方便使用。
QItemSelectionModel::ToggleCurrentToggle | Current切换和当前的组合,提供方便。
QItemSelectionModel::ClearAndSelectClear | Select清除和选择的组合,提供方便。

SelectionFlags 类型是QFlags<SelectionFlag> 的类型定义。它存储 SelectionFlag 值的 OR 组合。

属性文档

[read-only] selectedIndexes : const QModelIndexList

访问功能

QModelIndexList selectedIndexes() const

Notifier 信号:

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 中有任何项目被选中,则返回true ,给定的parent

注: 自 Qt 5.15 起,parent 的默认参数为空模型索引。

注: 可通过元对象系统和 QML 调用此函数。请参见Q_INVOKABLE

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

每当当前项目发生变化时,就会发出该信号。previous 模型的项目索引将被current 索引取代,成为选择的当前项目。

请注意,重置项目模型时不会发出此信号。

另请参阅 currentIndex()、setCurrentIndex() 和selectionChanged()。

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

如果current 项目发生变化,且其列与previous 当前项目的列不同,则会发出此信号。

请注意,重置项目模型时不会发出此信号。

另请参阅 currentChanged()、currentRowChanged()、currentIndex() 和setCurrentIndex()。

QModelIndex QItemSelectionModel::currentIndex() const

返回当前项目的模型项目索引,如果没有当前项目,则返回无效索引。

另请参见 setCurrentIndex()。

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

如果current 项目发生变化,且其行与previous 当前项目的行不同,则会发出此信号。

请注意,重置项目模型时不会发出此信号。

另请参阅 currentChanged()、currentColumnChanged()、currentIndex() 和setCurrentIndex()。

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

比较newSelectionoldSelection 这两个选择项,并将取消选择和选择的项目发送到selectionChanged() 中。

bool QItemSelectionModel::hasSelection() const

如果选择模型包含任何选定项,则返回true ,否则返回false

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

如果column 中的所有项目都被选中,则返回true ,给定parent

请注意,该函数通常比对同一列中的所有项目调用isSelected() 更快,而且不可选项目将被忽略。

注意: 自 Qt 5.15 起,parent 的默认参数为空模型索引。

注: 可通过元对象系统和 QML 调用此函数。请参见Q_INVOKABLE

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

如果row 中的所有项目都被选中,则返回true ,给定parent

请注意,该函数通常比对同一行中的所有项目调用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 时,会发出该信号。

另请参阅 model() 和setModel()。

[virtual slot] void QItemSelectionModel::reset()

清除选择模型。不发出任何信号。

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

如果row 中有任何项目被选中,则返回true ,给定的parent

注: 自 Qt 5.15 起,parent 的默认参数为空模型索引。

注: 可通过元对象系统和 QML 调用此函数。请参见Q_INVOKABLE

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

使用指定的command 选择项目selection ,并发出selectionChanged() 。

另请参阅 QItemSelectionModel::SelectionFlag

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

使用指定的command 选择模型项index ,并发出selectionChanged() 。

另请参阅 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 项的选中项。

请注意,当前索引的变化与选区无关。还要注意的是,重置项目模型时不会发出该信号。

保持选中状态但索引发生变化的项目不包括在selecteddeselected 中。因此,如果只有选中项的索引发生变化,在selecteddeselected 都为空的情况下,此信号可能会被发出。

注: 属性selectedIndexes 的通知信号。

另请参阅 select() 和currentChanged()。

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

设置模型项目index 为当前项目,并发出currentChanged() 信号。当前项目用于键盘导航和焦点指示;它与任何选定项目无关,尽管选定项目也可以是当前项目。

根据指定的commandindex 也可以成为当前选中项的一部分。

另请参阅 currentIndex() 和select()。

void QItemSelectionModel::setModel(QAbstractItemModel *model)

将模型设置为model 。将发出modelChanged() 信号。

另请参阅 model() 和modelChanged()。

© 2025 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.