0

大家好,我想知道是否可以将 qml 列表模型的列表元素保存到 JSON 文件中,如果可以,我该怎么做?

这是一个简单的代码,其中有一个带有列表模型的列表视图和一个用于创建 JSON 文件的按钮:

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


      }
    }
}

我知道如何解析 JSON 文件。我的问题诞生了,因为如果我更改列表元素角色之一的值(在这种情况下例如 colorCode)并且我重新启动应用程序,我希望模型具有新值。提前致谢。

4

0 回答 0