Adding a ListElemente to another List
-
wrote on 28 Jan 2016, 20:33 last edited by
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' list2.append(list1.get(0)) } } Text{ text: list2.get(0).name } }
-
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.
-
wrote on 28 Jan 2016, 21:54 last edited by A Former User
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
andlist2.get(0).name
tries to read a property ofundefined
.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. -
wrote on 28 Jan 2016, 23:12 last edited by
ah okay, I get it.
thanks for the explanation!
1/4