DelegateModel .get function returns nothing



  • 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



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

    visualModel.items.get(i).model


Log in to reply
 

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