(1)
/*
The QItemSelection class manages information about selected items in a model.
Detailed Description :
QItemSelection`描述了模型中被用户选中的项。
`QItemSelection`大体上是一个选择范围列表,参见QItemSelectionRange~。
它提供了创建和操作选择以及从模型中选择一系列项的功能。
QItemSelection类是Model/iew类之一,并且是Qt模型/视图框架的一部分。
可以构建和初始化一个项选择,以包含来自现有模型的一组项。
以下示例构建了一个选择,其包含从给定的模型中选取的一组项,起始点为左上角,结束点为右下角。
QItemSelection * selection = new QItemSelection(topLeft, bottomRight);
可以构建一个空的项目选择,然后在需要时进行填充。
因此,如果在构建项目选择时模型不可用,我们可以将上述代码重写为以下形式:
QItemSelection * selection = new QItemSelection();
...
selection->select(topLeft, bottomRight);
QItemSelection 通过处理选择范围而非为选择中的每个项目记录模型项索引,来节省内存并避免不必要的操作。
通常,该类的一个实例将包含一组不重叠的选择范围。
使用merge()函数可以将一个项选择合并到另一个选择中,而不会产生重叠范围。
使用split()函数可以根据另一个选择范围将一个选择范围分割成更小的范围。
*/
// We export each out-of-line method individually to prevent MSVC from
// exporting the whole QList class.
class QItemSelection : public QList<QItemSelectionRange>
{
public:
using QList<QItemSelectionRange>::QList; //这是列表 QList的构造函数
//Constructs an item selection that extends from the top-left model item,
// specified by the topLeft index,
// to the bottom-right item, specified by bottomRight.
Q_CORE_EXPORT
QItemSelection(const QModelIndex & topLeft, const QModelIndex & bottomRight);//构造函数
Q_CORE_EXPORT
void select (const QModelIndex & topLeft, const QModelIndex & bottomRight);
//Adds the items in the range that extends from the top-left model item,
// specified by the topLeft index,
// to the bottom-right item, specified by bottomRight to the list.
//Note: topLeft and bottomRight must have the same parent.
// reusing QList::swap() here is OK!
//Returns true if the selection contains the given index; otherwise returns false.
Q_CORE_EXPORT bool contains(const QModelIndex & index) const; //是否包含形参索引
Q_CORE_EXPORT QModelIndexList indexes() const; //返回所有的本选择条目的索引。
//Returns a list of model indexes that correspond to the selected items.
//Merges the other selection with this QItemSelection using the command given.
//This method guarantees that no ranges are overlapping.
//Note that only QItemSelectionModel::Select,
// QItemSelectionModel::Deselect, and QItemSelectionModel::Toggle are supported.
Q_CORE_EXPORT void merge(const QItemSelection & other,
QItemSelectionModel::SelectionFlags command);
/*
enum QItemSelectionModel::SelectionFlag {
NoUpdate = 0x0000,
Clear = 0x0001,
Select = 0x0002,
Deselect = 0x0004,
Toggle = 0x0008,
Current = 0x0010,
Rows = 0x0020,
Columns = 0x0040,
SelectCurrent = Select | Current,
ToggleCurrent = Toggle | Current,
ClearAndSelect = Clear | Select
};
*/
//把选择范围里 range里的也属于选择范围 other的条目全部删除,得到的新条目范围对象存入形参 result中。
Q_CORE_EXPORT //注意,这是个静态成员函数
static void split(const QItemSelectionRange & range,
const QItemSelectionRange & other,
QItemSelection * result);
//Splits the selection range using the selection other range.
//Removes all items in other from range and puts the result in result.
//This can be compared with the semantics of the subtract operation of a set.
//这可以与集合的减法操作的语义进行比较。
}; //class QItemSelection : QList<QItemSelectionRange>
Q_DECLARE_SHARED(QItemSelection)
(2)
谢谢