Solved QML ListView: model like Array of Arrays. How?
-
Hello all!
Is there any way to pass array of arrays to QML ListView and how to access data from this array-of-arrays?
Something like this:property var pData: [ ["Field_0-01","Field_0-02","Field_0-03","Field_0-04","Field_0-05"], ["Field_1-01","Field_1-02","Field_1-03","Field_1-04","Field_1-05"], ["Field_2-01","Field_2-02","Field_2-03","Field_1-04","Field_2-05"], ["Field_3-01","Field_3-02","Field_3-03","Field_1-04","Field_3-05"], ["Field_4-01","Field_4-02","Field_4-03","Field_1-04","Field_4-05"], ["Field_5-01","Field_5-02","Field_5-03","Field_1-04","Field_5-05"], ["Field_6-01","Field_6-02","Field_6-03","Field_1-04","Field_6-05"], ["Field_7-01","Field_7-02","Field_7-03","Field_1-04","Field_7-05"] ]; ... ListView { ... model: pData; ... }
For me works only direct access to pData by currentIndex of ListView:
oApplicationWindow.pData[index][0]
But is there way to access it through "model"?
-
This might work:
modelData[0]
-
@sierdzio Thx. It works.
-
@sierdzio Do you know which method should be implemented for the same functionality if I am developing my own/custom data model based on QAbstractListModel?
-
Declare roles by reimplementing
roleNames()
, then, indata()
use these roles to return your data. In QML, use the names specified byroleNames()
to access the data.So, for a very short example (pseudocode, sorry I don't have time to check all params etc.):
QHash<> roleNames() { return { Qt::UserRole + 1, "myRole" }; } QVariant data() { if (role == Qt::UserRole+1) { return QVariantList{ "A", "B", "C" }; } } /// QML: console.log(myRole[0] // Will print "A")
-
@sierdzio Thx. I've got roleNames missed. It's mean "modelData" just a role name. I though it's Qt-system value. Briefly all of it looks like model within only one role where stored QVariantList that is transforming into JS-Array in QML.