Important: Please read the Qt Code of Conduct -

Adding a ListElemente to another List

  • I'm trying to add a ListElement to another List.
    The strange thing is that List2.count gets higher, but I can not put the name into the text. I hope you can help me.
    Here is the Code:

    import QtQuick 2.0
    Rectangle {
        width: 100
        height: 62
        ListModel {
            id: list1
            ListElement {
                    name: "Apple"
                    cost: 2.45
        ListModel {
            id: list2
        MouseArea {
               anchors.fill: parent
               onClicked: { parent.color = 'red'
            text: list2.get(0).name

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Your Text element gets it's content from the first element of list2 but as written here, changes to list2 won't affect the content of the Text element.

    You should rather use a ListView to show the content of your ListModel.

  • Hi!

    text: list2.get(0).name

    First of all this produces a runtime error when your program starts because at that time list2 is empty thus list2.get(0) evaluates to undefined and list2.get(0).name tries to read a property of undefined.

    So you may ask: "But why isn't the text property updated once list2.get(0).name becomes valid? I mean, name is a property so we have a property binding!" The answer is: No, we don't have a property binding. The property binding is created on program start-up. As already mentioned at that time there is no name property to bind to because list2.get(0) is not an object. So your statement is not a property binding and will never be reevaluated.

  • ah okay, I get it.
    thanks for the explanation!

Log in to reply