How to save and restore the content of a ListModel?

前端 未结 1 1395
野趣味
野趣味 2020-12-11 10:04

I am able to save settings for list items which is statically created using Component.onComponent method. But Settings for statically created list items take af

相关标签:
1条回答
  • 2020-12-11 10:48

    Quite curious that you expect that saving a single integer value will somehow be able to store the content of an arbitrary data model... It doesn't work even for the static model data, it is only "restored" because it is static - it is part of the code, you are not really saving and restoring anything.

    If you want to store all that data, you will have to serialize it when your app quits, and deserialize it when the app starts.

    You could still use Settings, but to store a string value, that will represent the serialized data.

    The easiest way to do it is to transfer the model items back and forth with a JS array, this way the JS JSON object functionality can be used to easily serialize and deserialize the data:

    import QtQuick 2.9
    import QtQuick.Controls 2.2
    import QtQuick.Window 2.3
    import Qt.labs.settings 1.0
    
    ApplicationWindow {
      id: main
      width: 640
      height: 480
      visible: true
    
      property string datastore: ""
    
      Component.onCompleted: {
        if (datastore) {
          dataModel.clear()
          var datamodel = JSON.parse(datastore)
          for (var i = 0; i < datamodel.length; ++i) dataModel.append(datamodel[i])
        }
      }
    
      onClosing: {
        var datamodel = []
        for (var i = 0; i < dataModel.count; ++i) datamodel.push(dataModel.get(i))
        datastore = JSON.stringify(datamodel)
      }
    
      Settings {
        property alias datastore: main.datastore
      }
    
      ListView {
        id: view
        anchors.fill: parent
        model: ListModel {
          id: dataModel
          ListElement { name: "test1"; value: 1 }
        }
        delegate: Text {
          text: name + " " + value
        }
      }
    
      MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: {
          if (mouse.button === Qt.LeftButton) {
            var num = Math.round(Math.random() * 10)
            dataModel.append({ "name": "test" + num, "value": num })
          } else if (dataModel.count) {
            dataModel.remove(0, 1)
          }
        }
      }
    }
    

    The application begins with a single data model value, more data items can be added or removed by pressing the left and right mouse button respectively.

    As long as the application is closed properly, the data model will be copied into an array, which will be serialized to a string, which will be stored by the Settings element. So upon relaunching the app, if the data string is present, the model is cleared to remove the initial value so it is not duplicated, the data string is deserialized back into an array, which is iterated to restore the content of the data model. Easy peasy.

    Of course, you could also use the LocalStorage API as well, or even write a simple file reader and writer by exposing a C++ object to QML. All this approach needs is to be able to store and retrieve a single string.

    0 讨论(0)
提交回复
热议问题