简体   繁体   中英

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. r stands for the row and ec is for the column. I notice from examples that they add QModelIndex() on the third parameter. The third parameter stands for the parent index.

tm is just a simple QTableView . Is it really necessary to provide QModelIndex ? I tried reading the Qt manual but i can't seem to find any simple explanations when to supply a parent index.

Quoting from the documentation :

An invalid model index can be constructed with the QModelIndex constructor. 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.

So if you do not have nested data (like in your QTableView ), the parent index will always be an invalid one.

The signature of QAbstractItemModel::index is:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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