Important: Please read the Qt Code of Conduct -

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: [
    ListView {
    	model: pData;

    For me works only direct access to pData by currentIndex of ListView:


    But is there way to access it through "model"?

  • Moderators

    This might work:


  • @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?

  • Moderators

    Declare roles by reimplementing roleNames(), then, in data() use these roles to return your data. In QML, use the names specified by roleNames() 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.

Log in to reply