pablodepas87
pablodepas87

Reputation: 307

How i can save into JSON file a QML list model?

Hello everyone i want to know if is possible save into JSON file the list element of a qml list model and if it is possible how i can do it?

This is a simple code where there are a list view with list model and a button for create the JSON file :

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ListModel {
       id:myModel
       ListElement {colorCode: "#d1d1d1" ; nameRectangle:"row1" }
       ListElement {colorCode: "#dbdbdb" ; nameRectangle:"row2" }
       ListElement {colorCode: "#85c7f2" ; nameRectangle:"row3" }
       ListElement {colorCode: "#636363" ; nameRectangle:"row4" }
       ListElement {colorCode: "#4c4c4c" ; nameRectangle:"row5" }
       ListElement {colorCode: "#634f4f" ; nameRectangle:"row6" }
       ListElement {colorCode: "#ddeffb" ; nameRectangle:"row7" }


    }

    ListView{
       id:list
       width: parent.width
       height: parent.height*0.90
       signal updateModel()
       onUpdateModel: {
           list.model=0
           list.model=myModel

       }
       delegate:  Rectangle {

           color: colorCode
           width:list.width*0.95
           height: list.height/4
           Text{

             anchors.fill: parent
             text:nameRectangle
             font.pixelSize: 20

           }

           MouseArea {
            anchors.fill: parent
            onClicked:{ myModel.setProperty(index,colorCode,"#ff0000")
               list.updateModel()
             }

           }
         }
       model:myModel
    }

    Button{

      anchors.top:myModel.bottom
      anchors.bottom: parent.bottom
      width:parent.width
      text:"Save into json file"
      onClicked: {

          //how i can save the list model into json File


      }
    }
}

I know how to parse JSON file. My question born because if i change a value of one of the role of a list element ( in this case for example the colorCode) and i restart the application i want that the model has the new value . Thanks in advance.

Upvotes: 0

Views: 971

Answers (0)

Related Questions