本页内容

QItemSelection Class

QItemSelection 类用于管理模型中已选中项的相关信息。更多内容...

头文件: #include <QItemSelection>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承自: QList

公共函数

QItemSelection()
QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight)
bool contains(const QModelIndex &index) const
QModelIndexList indexes() const
void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command)
void select(const QModelIndex &topLeft, const QModelIndex &bottomRight)

静态公共成员

void split(const QItemSelectionRange &range, const QItemSelectionRange &other, QItemSelection *result)

详细说明

QItemSelection 描述了模型中被用户选中的项目。QItemSelection 基本上是一个选择范围列表,详见QItemSelectionRange 。它提供了用于创建和操作选择以及从模型中选择项目范围的函数。

QItemSelection 类是模型/视图类之一,属于 Qt的模型/视图框架。

可以构造并初始化一个项目选择,使其包含来自现有模型的某个项目范围。以下示例构造了一个选择,其中包含来自给定model 的项目范围,起始于topLeft ,结束于bottomRight 。

QItemSelection *selection = new QItemSelection(topLeft, bottomRight);

可以创建一个空的项目选择,并随后根据需要填充其内容。因此,如果创建项目选择时模型不可用,我们可以按以下方式重写上述代码:

QItemSelection *selection = new QItemSelection();
//...
selection->select(topLeft, bottomRight);

QItemSelection 通过使用选择范围(而非记录选择中每个项的模型项索引)来节省内存并避免不必要的工作。通常,该类的实例将包含一组互不重叠的选择范围。

使用 `merge()` 将一个项目选择合并到另一个项目选择中,同时避免产生重叠范围。使用 `split()` 根据另一个选择范围,将一个选择范围拆分为较小的范围。

另请参阅 “模型/视图编程”和QItemSelectionModel 。

成员函数文档

[implicit] QItemSelection::QItemSelection()

创建一个空选择集。

QItemSelection::QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight)

构建一个项目选择区域,该区域从左上角的模型项目(由topLeft 索引指定)延伸至右下角的项目(由bottomRight 指定)。

bool QItemSelection::contains(const QModelIndex &index) const

如果所选内容包含给定的index ,则返回true ;否则返回false 。

QModelIndexList QItemSelection::indexes() const

返回一个与所选项目相对应的模型索引列表。

void QItemSelection::merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command)

使用给定的command ,将other 中的选择与QItemSelection 中的选择合并。此方法可确保所有范围互不重叠。

请注意,仅支持QItemSelectionModel::Select 、QItemSelectionModel::Deselect 和QItemSelectionModel::Toggle 。

另请参阅 split()。

void QItemSelection::select(const QModelIndex &topLeft, const QModelIndex &bottomRight)

将范围内的项目(从由索引topLeft 指定的左上角模型项目,到由bottomRight 指定的右下角项目)添加到列表中。

注意: topLeft 和bottomRight 必须具有相同的父项。

[static] void QItemSelection::split(const QItemSelectionRange &range, const QItemSelectionRange &other, QItemSelection *result)

使用选区other 的范围将选区range 进行拆分。从range 中移除other 中的所有项目,并将结果放入result 中。这可以与集合的“减法”运算的语义进行类比。

另请参阅 merge()。

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