Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved DelegateModel .get function returns nothing

    QML and Qt Quick
    delegatemodel delegatemodelgr
    2
    2
    882
    Loading More Posts
    • 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.
    • mpnHar
      mpnHar last edited by

      Hi there,

      I'm having problems getting the values of particular rows from a DelegateModel. The get() function works fine on the ListModel. However when I try call get() on the DelegateModel it returns nothing, {}.

      I've included a simplified example below. In which I create a ListModel, attach it to DelegateModel and then print the values of the model using ListModel.get() and DelegateModel.items.get(). The DelegateModel finds the right number of items but in for each get call, isn't able to find anything.

      What have I missed?

      import QtQuick 2.0
      import QtQml.Models 2.1
      
      Item {
          id: mainContainer
          ...
      
          Component {
              id: d
              ...
          }
      
          ListModel {
              id: mylist
              ListElement {
                  name: "Polly"
                  type: "Parrot"
                  age: 12
                  size: "Small"
              }
              ListElement {
                  name: "Penny"
                  type: "Turtle"
                  age: 4
                  size: "Small"
              }
              ListElement {
                  name: "Warren"
                  type: "Rabbit"
                  age: 2
                  size: "Small"
              }
              ListElement {
                  name: "Spot"
                  type: "Dog"
                  age: 9
                  size: "Medium"
              }
          }
      
          DelegateModel {
              id: visualModel
              model: mylist
              delegate: d
              Component.onCompleted: {
                  //Iterate over the ListModel and show that the values exist
                  console.debug("VISUAL MODEL completed... dumping mylist");
                  for (var i = 0; i < mylist.count; i++)
                  {
                      console.debug("NAME: ", mylist.get(i).name);
                      console.debug("TYPE: ", mylist.get(i).type);
                  }
      
                   //Iterate over the DelegateModel.items and attempt to print
                  console.debug("... dumping visualModel.items");
                  for (var i = 0; i < visualModel.items.count; i++)
                  {
                      console.debug("Print item ", i);
                      console.debug(JSON.stringify(visualModel.items.get(i)));
                      console.debug("NAME:", visualModel.items.get(i).name);
                  }
              }
          }
      }
      

      Returns:
      qml: VISUAL MODEL completed... dumping mylist
      qml: NAME: Polly
      qml: TYPE: Parrot
      qml: NAME: Penny
      qml: TYPE: Turtle
      qml: NAME: Warren
      qml: TYPE: Rabbit
      qml: NAME: Spot
      qml: TYPE: Dog
      qml: ... dumping visualModel.items
      qml: Print item 0
      qml: {}
      qml: NAME: undefined
      qml: Print item 1
      qml: {}
      qml: NAME: undefined
      qml: Print item 2
      qml: {}
      qml: NAME: undefined
      qml: Print item 3
      qml: {}
      qml: NAME: undefined

      1 Reply Last reply Reply Quote 0
      • JimmyA
        JimmyA last edited by

        In your visualModel when accessing the items, try to use .model after the get(), like so:

        visualModel.items.get(i).model

        1 Reply Last reply Reply Quote 1
        • First post
          Last post