Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Saving ListModel content to a file using JSON.stringify() or an other solution
QtWS25 Last Chance

Saving ListModel content to a file using JSON.stringify() or an other solution

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 311 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Allon
    wrote on last edited by Allon
    #1

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved