Solved How does the Append method add attributes to the ListModel?
-
fruitModel.append({ "name": "Apple", "description": "Core"})
ListModel { id: fruitModel ListElement { name: "Apple" attributes: [ ListElement { description: "Core" }, ] } }
-
@mirro Sorry read the documentation too quickly.
To answer your question, you forgot the " around attributes and you forgot the : to tell that the first word is a property, then you forgot the brackets to tell that it is an array.
{ "name": "Apple", "attributes": [{ description: "table" }]}
instead of
{"name": "Apple", attributes{"description": "Core"}Here is a code to illustrate it:
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") ListModel { id: fruitModel ListElement { name: "Apple" attributes: [ ListElement { description: "Core" } ] } } Button { id: btn onClicked: { fruitModel.append({ "name": "Apple", "attributes": [{ description: "table" }]}) var keysList = JSON.stringify(fruitModel) console.log(keysList) console.log(JSON.stringify(fruitModel)) } } ListView { width: 180; height: 200 anchors.top: btn.bottom model: fruitModel delegate: Text { text: name + ": " + attributes.get(0).description } } }
Regards,
-
What is exactly your question?
Regards, -
@Allon What is wrong with the following QML code syntax?
fruitModel.append({ "name": "Apple", attributes{"description": "Core"}})
ListModel {
id: fruitModelListElement { name: "Apple" attributes: [ ListElement { description: "Core" }, ] }
}
-
There is no append() function for ListModel
-
https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html
Methods
append(dict)
clear()
object get(index)
insert(index, jsobject dict)
move(from, int to, int n)
remove(index, int count = 1)
set(index, jsobject dict)
setProperty(index, string property, variant value)
sync() -
@mirro Sorry read the documentation too quickly.
To answer your question, you forgot the " around attributes and you forgot the : to tell that the first word is a property, then you forgot the brackets to tell that it is an array.
{ "name": "Apple", "attributes": [{ description: "table" }]}
instead of
{"name": "Apple", attributes{"description": "Core"}Here is a code to illustrate it:
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") ListModel { id: fruitModel ListElement { name: "Apple" attributes: [ ListElement { description: "Core" } ] } } Button { id: btn onClicked: { fruitModel.append({ "name": "Apple", "attributes": [{ description: "table" }]}) var keysList = JSON.stringify(fruitModel) console.log(keysList) console.log(JSON.stringify(fruitModel)) } } ListView { width: 180; height: 200 anchors.top: btn.bottom model: fruitModel delegate: Text { text: name + ": " + attributes.get(0).description } } }
Regards,