Solved QtQuick 2 TableView row number
-
Hi
I'm using a TableView and I want to get the row number of a delegate. I assumed the index would consist of a row and column, but it turns out it is a number. The index property does follow some pattern, but I can't find it documented anywhere.
I have a table with 4 columns, and the index property contains the values as indicated in the table:
0, 3000, 6000, 9000 1, 3001, 6001, 9001 2, 3002, 6002, 9002
Can I assume the index values will always follow this pattern and map this to row and column numbers or is there some other way to determine the row number of a delegate in a TableView?
-
There are actually row and column properties that are exposed! I don't know how I missed this.
Some digging before I realized this, to explain the integer index:
So it seems Qt generates a unique index for each item based on the row count. The items in the first column are numbered 0...rowCount -1, the seconds column rowCount...rowCount + rowCount -1, etc.Browsing through the Qt code, it seems this mapping between a QModelIndex and the QML (int) index is maintained in QQmlAdaptorModel.
@jay1 I don't need a persistent index, I just want to get to the current index.
@6thC Yes, I have a QAbstractItemModel derived model.
-
In TableView, the model index is exposed as an accessible index property.
In QModelIndex, Each top-level item in a model is represented by a model index that does not have a parent index. Each top level item start with a zero index i.e. as 0, 3000, 6000, 9000 ..... but this is not persisted. If we want to persist the index, we need to use the QPeristentModelIndex, this provides a way to store the index by an application and can be accessible. Using C++ for assigning the indexes is an option to try.
For reference:
https://doc.qt.io/qt-5/qml-qtquick-tableview.html
https://doc.qt.io/qt-5/qpersistentmodelindex.html -
QAbstractTableModel https://doc.qt.io/qt-5/qabstracttablemodel.html
QSortFilterProxyModel https://doc.qt.io/qt-5/qsortfilterproxymodel.htmlFor TableView I just bit the bullet and went the full model and haven't looked back. It might take a day or a week if you struggle. But once it's in your head it's really simple and works very well with QML and std:: containers. Or at least, that's been my experience.
So my index just matches my std:: container data / index and that keeps things rather simple.
-
There are actually row and column properties that are exposed! I don't know how I missed this.
Some digging before I realized this, to explain the integer index:
So it seems Qt generates a unique index for each item based on the row count. The items in the first column are numbered 0...rowCount -1, the seconds column rowCount...rowCount + rowCount -1, etc.Browsing through the Qt code, it seems this mapping between a QModelIndex and the QML (int) index is maintained in QQmlAdaptorModel.
@jay1 I don't need a persistent index, I just want to get to the current index.
@6thC Yes, I have a QAbstractItemModel derived model.