简体   繁体   English

获取索引时qmodelindex的父参数

[英]parent parameter for qmodelindex when getting the index

Here is the line of code: 这是代码行:

QModelIndex id = tm->index(r, ec, QModelIndex());

So i have my own model which is tm and i am trying to access its index. 所以我有自己的模型tm ,我正在尝试访问其索引。 r stands for the row and ec is for the column. r代表行, ec代表列。 I notice from examples that they add QModelIndex() on the third parameter. 我从示例中注意到,他们在第三个参数上添加了QModelIndex() The third parameter stands for the parent index. 第三个参数代表父索引。

tm is just a simple QTableView . tm只是一个简单的QTableView Is it really necessary to provide QModelIndex ? 是否真的有必要提供QModelIndex I tried reading the Qt manual but i can't seem to find any simple explanations when to supply a parent index. 我尝试阅读Qt手册,但何时提供父级索引似乎找不到任何简单的解释。

Quoting from the documentation : 引用文档

An invalid model index can be constructed with the QModelIndex constructor. 可以使用QModelIndex构造函数构造无效的模型索引。 Invalid indexes are often used as parent indexes when referring to top-level items in a model. 当引用模型中的顶级项目时,无效索引通常用作父索引。

... ...

Each top-level item in a model is represented by a model index that does not have a parent index - in this case, parent() will return an invalid model index, equivalent to an index constructed with the zero argument form of the QModelIndex() constructor. 模型中的每个顶级项都由没有父索引的模型索引表示-在这种情况下,parent()将返回无效的模型索引,该索引等效于使用QModelIndex()的零参数形式构造的索引)的构造函数。

So if you do not have nested data (like in your QTableView ), the parent index will always be an invalid one. 因此,如果没有嵌套数据(如QTableView嵌套数据),则父索引将始终是无效的。

The signature of QAbstractItemModel::index is: QAbstractItemModel::index的签名是:

QModelIndex QAbstractItemModel::index(int row, int column, const QModelIndex & parent = QModelIndex()) const

The last parameter is optional, in your case you can just omit it to supply an invalid model index. 最后一个参数是可选的,在您的情况下,您可以省略该参数以提供无效的模型索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM