[RESOLVED] Access to json model from ListView delegate



  • Hi guys,
    first of all, sorry for my English, and take in mind that I'm Qt newbie
    I have problem with my ListView
    I try populate data from json but can't get value from delegate
    I'll take code from example, but it's not work
    thank you for help
    here small example
    @
    function prepareWarriorList() {
    var obj = JSON.parse(Qt.core.prepareWarriorJSON());
    // there all fine
    console.log(obj) // qml: [[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]]
    console.log(obj[0]) // qml: [object Object]
    console.log(obj[0].code) // qml: military_volunteers
    return obj;
    }

    ScrollView {
    id: svWarrior

    ListView {
    id: lvWarrior
    anchors.fill: parent
    model: prepareWarriorList() // parse JSON and write data to log
    focus: true
    delegate: Item {
    id: warriorDelegate
    width: svWarrior.width - 20
    height: 0.25 * dialogWindow.height

    Row {
    Image {
    height: 0.25 * dialogWindow.height
    width: 0.25 * dialogWindow.height
    // but there model.code === undefined
    source: "qrc:///res/img/" + model.code + ".png"
    }

    Column {
     height: 0.25 * dialogWindow.height
    
     Text {
      text: dict.getStringByCode("CurrentCount") + ": " + model.count
     }
    }
    

    }
    }
    }
    }
    @



  • Try using modelData.count instead of model.count, because you have a simple array of objects.

    From docs:
    Models that do not have named roles (such as the ListModel shown below) will have the data provided via the modelData role.



  • [quote author="chocis" date="1423762598"]Try using modelData.count instead of model.count[/quote]
    yes, thank you, it's working, thank you very much


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.