1

I've some javascript data containing a list of objects which contains other objects and arrays, and append that to a ListModel.

Such as (assume that the data is generated elsewhere and I would like to avoid its structure):

import QtQuick 2.0

Rectangle {
  width: 600
  height: 300

  ListView {
    anchors.fill: parent

    model: ListModel {
      id: listModel
    }

    delegate: Text {
      text: 'key: '+ key + ', subdict["subkey1"]: ' + subdict.subkey1 +
            ', sublist.count: '+ sublist.count + ', sublist[0]: '+ sublist[0] +
            ', sublist.get(0): '+ sublist.get(0)
      wrapMode: Text.WordWrap
      width: parent.width
      color: index % 2 == 0 ? "darkgreen" : "brown"
   }

   Component.onCompleted: {
    var values = [
                   {'key': 'foo1', 'subdict': {'subkey1': 'subfoo1', 'subkey2': 'subbar1'},
                    'sublist': ['subvalue1', 'subvalue2']},
                   {'key': 'foo2', 'subdict': {'subkey1': 'subfoo2', 'subkey2': 'subbar2'},
                    'sublist': ['subvalue3', 'subvalue4', 'subvalue5']},
                   {'key': 'foo3', 'subdict': {'subkey1': 'subfoo3', 'subkey2': 'subbar4'},
                    'sublist': []}
                 ]

    for (var i in values)
      listModel.append(values[i]);
   }
  }
}

Most parts work well, but I can't find a way to access to the sublist elements. All I can get is the count of them as they're actually a new ListModel, but since I can't use something such as sublist.get(0) it's seems impossible to get the actual content.

Is this a bug, or am I missing something?

3
  • Probably related to QTBUG-35891 (and so QTBUG-12117)? Commented Jan 12, 2015 at 13:35
  • 1
    Is the model read-only? If this is your case, you can use a javascript structure (var values =[...]) like model directly, and access every item in delegate with "modelData" keyword. Remember that this way any changes in model won't be painted, so you should refresh the whole listview. Commented Jan 12, 2015 at 15:11
  • Thanks, this actually does the trick. I don't need to get the model to be updated dynamically, so it's good for me. Commented Jan 13, 2015 at 12:05

1 Answer 1

4

This answer actually comes from Jairo's comment, and it's a good workaround if the model can be read-only and you don't need dynamic model updates.

And the solution is basically just setting the javascript object as the listview model, and accessing to elements in delegate through the modelData keyword.

import QtQuick 2.0

Rectangle {
  width: 600
  height: 300

  ListView {
    anchors.fill: parent

    delegate: Text {
      text: 'key: '+ modelData.key + ', subdict["subkey1"]: ' + modelData.subdict.subkey1 +
            ', sublist.length: '+ modelData.sublist.length + ', sublist[0]: '+ modelData.sublist[0] +
            ', sublist: ['+ modelData.sublist + ']'
      wrapMode: Text.WordWrap
      width: parent.width
      color: index % 2 == 0 ? "darkgreen" : "brown"
   }

   Component.onCompleted: {
    model = [
              {'key': 'foo1', 'subdict': {'subkey1': 'subfoo1', 'subkey2': 'subbar1'},
              'sublist': ['subvalue1', 'subvalue2']},
              {'key': 'foo2', 'subdict': {'subkey1': 'subfoo2', 'subkey2': 'subbar2'},
              'sublist': ['subvalue3', 'subvalue4', 'subvalue5']},
              {'key': 'foo3', 'subdict': {'subkey1': 'subfoo3', 'subkey2': 'subbar4'},
              'sublist': []}
            ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.