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. DelegateModel .get function returns nothing
QtWS25 Last Chance

DelegateModel .get function returns nothing

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
delegatemodeldelegatemodelgr
2 Posts 2 Posters 1.1k Views
  • 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.
  • M Offline
    M Offline
    mpnHar
    wrote on 12 Apr 2016, 17:50 last edited by
    #1

    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
    0
    • J Offline
      J Offline
      JimmyA
      wrote on 8 Aug 2016, 12:34 last edited by
      #2

      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
      1

      • Login

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