Saving ListModel content to a file using JSON.stringify() or an other solution
Unsolved
QML and Qt Quick
-
Hi,
I want to be able to save the content of a ListModel in a file in order to retrieve it.
I have been able to do it with a not nested ListModel using JSON.stringify().However my ListModel contains nested data
Here is a simplified version of it;
import QtQuick 2.12 import QtQuick.Window 2.12 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") ListModel { id: fruitModel ListElement { name: "Apple" cost: 2.45 attributes: [ ListElement { description: "Core" }, ListElement { description: "Deciduous" } ] } ListElement { name: "Orange" cost: 3.25 attributes: [ ListElement { description: "Citrus" } ] } ListElement { name: "Banana" cost: 1.95 attributes: [ ListElement { description: "Tropical" }, ListElement { description: "Seedless" } ] } } Component.onCompleted: console.log(JSON.stringify(fruitModel.get(0), ['name', 'cost', 'attributes', 'description'])) }
Output for the first element gives:
{"attributes":{"objectName":"","count":2,"dynamicRoles":false},"cost":2.45,"name":"Apple"}JSON.stringify displays nested objects and not their content.
Is there an other way to use JSON.stringify() to overcome this problem?
If not what could I do?Thanks in advance,
Emmanuel