Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [RESOLVED] Access to json model from ListView delegate
Forum Updated to NodeBB v4.3 + New Features

[RESOLVED] Access to json model from ListView delegate

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Galbarad
    wrote on last edited by
    #1

    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
     }
    }
    

    }
    }
    }
    }
    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chocis
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Galbarad
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved