QModelIndex 类
QModelIndex类用于定位数据模型中的数据。
Header: | #include < QModelIndex > |
---|---|
qmake: | QT += core |
Inherits: | |
Inherited By: |
详述
该类用作从QAbstractItemModel派生的项模型的索引。项视图、委托和选择模型使用索引来定位模型中的项。
新的QModelIndex对象由模型使用QAbstractItemModel::createIndex()函数创建。可以使用QModelIndex构造函数构造无效的模型索引。在引用模型中的顶级项时,无效索引通常用作父索引。
模型索引引用模型中的项,并包含指定它们在那些模型中的位置所需的所有信息。每个索引位于给定的行和列中,并且可以具有父索引;使用row()、column()和parent()来获取这些信息。模型中的每个顶级项都由一个没有父索引的模型索引表示——在本例中,parent()将返回一个无效的模型索引,相当于用QModelIndex()构造函数的零参数形式构造的索引。
要获得引用模型中现有项的模型索引,使用所需的行和列值以及父模型索引调用QAbstractItemModel::index()。当引用模型中的顶级项时,提供QModelIndex()作为父索引。
函数model() 的作用是:返回索引引用的QAbstractItemModel模型。函数child()用于检查模型中索引下的项。sibling()函数允许您在与索引同级的模型中遍历项目。
注: 模型所以应立即使用,然后丢弃。在调用更改模型结构或删除项的模型函数后,不应依赖索引来保持有效。如果您需要随着时间的推移保持模型索引,请使用QPersistentModelIndex。
公共函数
- QModelIndex()
- int column() const
- int row() const
- QVariant data(int role = Qt::DisplayRole) const 返回相应的数据 不需要具体项目,方便函数吧
- Qt::ItemFlags flags() const
- quintptr internalId() const
- void * internalPointer() const
- bool isValid() const
- const QAbstractItemModel * model() const
- QModelIndex parent() const
- QModelIndex sibling(int row, int column) const
- QModelIndex siblingAtColumn(int column) const
- QModelIndex siblingAtRow(int row) const
- bool operator!=(const QModelIndex &other) const
- bool operator<(const QModelIndex &other) const
- bool operator==(const QModelIndex &other) const
相关的非成员类
typedef QModelIndexList Synonym for QList< QModelIndex >
参见
- 模型/视图编程
- QPersistentModelIndex
- QAbstractItemModel。