Carlos Miguel Colanta
Carlos Miguel Colanta

Reputation: 2823

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.

Upvotes: 0

Views: 900

Answers (1)

m.s.
m.s.

Reputation: 16324

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.

Upvotes: 2

Related Questions