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. [QML] Persistance of ListModel data

[QML] Persistance of ListModel data

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 1.7k 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by aha_1980
    #1

    Hi,
    I have a qml page that shows a ListView, the model is updated at run time.
    That page is loaded with a Loader (List_page.qml) .

    How to keep the modelData in memory when i change the loaders source ? I want to assign the same data when List_page.qml is reloaded again.

    Page{
       Loader{
         source : showList  ? "List_page.qml" : "Base_page.qml"
       }
    }
    

    Is this possible with QML Settings type ? I tryed this with no success

        Settings {
            id:_stg
            property alias mod : fm
        }
        ListView{
            id:fList
            model:_stg.mod        
        }
       ListModel{
            id:fm
        }
    
    ODБOïO DiracsbracketD 2 Replies Last reply
    0
    • ODБOïO ODБOï

      Hi,
      I have a qml page that shows a ListView, the model is updated at run time.
      That page is loaded with a Loader (List_page.qml) .

      How to keep the modelData in memory when i change the loaders source ? I want to assign the same data when List_page.qml is reloaded again.

      Page{
         Loader{
           source : showList  ? "List_page.qml" : "Base_page.qml"
         }
      }
      

      Is this possible with QML Settings type ? I tryed this with no success

          Settings {
              id:_stg
              property alias mod : fm
          }
          ListView{
              id:fList
              model:_stg.mod        
          }
         ListModel{
              id:fm
          }
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      I found this solution https://stackoverflow.com/questions/48730166/how-to-save-and-restore-the-content-of-a-listmodel by @dtech

      when my ListView page is destroyed datastore holds the list data but when i reload the page data is not restaured, so it looks like i have a issue with the Settings type..

      Can someone tell me what could be the issue here please ?

      import Qt.labs.settings 1.0
      
       property string datastore: ""
      
        Component.onCompleted: {
          if (datastore) { // will not enter here when reload the page
            dataModel.clear()
            var datamodel = JSON.parse(datastore)
            for (var i = 0; i < datamodel.length; ++i) dataModel.append(datamodel[i])
          }
        }
      
        Component.onDestruction : {
          var datamodel = []
          for (var i = 0; i < dataModel.count; ++i) datamodel.push(dataModel.get(i))
          datastore = JSON.stringify(datamodel)
          console.log(datamodel) 
        }
      
        Settings {
          property alias datastore: main.datastore
        }
      
      

      Qt 5.11.0
      Mingw32

      1 Reply Last reply
      0
      • ODБOïO ODБOï

        Hi,
        I have a qml page that shows a ListView, the model is updated at run time.
        That page is loaded with a Loader (List_page.qml) .

        How to keep the modelData in memory when i change the loaders source ? I want to assign the same data when List_page.qml is reloaded again.

        Page{
           Loader{
             source : showList  ? "List_page.qml" : "Base_page.qml"
           }
        }
        

        Is this possible with QML Settings type ? I tryed this with no success

            Settings {
                id:_stg
                property alias mod : fm
            }
            ListView{
                id:fList
                model:_stg.mod        
            }
           ListModel{
                id:fm
            }
        
        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by Diracsbracket
        #3

        @LeLev
        Either you move the declaration of the ListModel to a persistent part of your app,
        or you can save the data in the Settings, but not directly.

        You will need to serialize the data as a long string... to save it and then deserialize it to get it back.
        https://stackoverflow.com/questions/48730166/how-to-save-and-restore-the-content-of-a-listmodel

        1 Reply Last reply
        2
        • ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #4

          Finally i just followed @Diracsbracket suggestion and moved the model to a persistent part of my application. Thx @Diracsbracket

          DiracsbracketD 1 Reply Last reply
          0
          • ODБOïO ODБOï

            Finally i just followed @Diracsbracket suggestion and moved the model to a persistent part of my application. Thx @Diracsbracket

            DiracsbracketD Offline
            DiracsbracketD Offline
            Diracsbracket
            wrote on last edited by Diracsbracket
            #5

            @LeLev
            I didn't see your post where you tried the same link that I mentioned. Since it didn't work for you, did you set all of:

            app.setOrganizationName()
            app.setOrganizationDomain() 
            app.setApplicationName() 
            

            in your main.cpp? This seems to be needed for Settings to work, since you target Windows.

            ODБOïO 1 Reply Last reply
            1
            • DiracsbracketD Diracsbracket

              @LeLev
              I didn't see your post where you tried the same link that I mentioned. Since it didn't work for you, did you set all of:

              app.setOrganizationName()
              app.setOrganizationDomain() 
              app.setApplicationName() 
              

              in your main.cpp? This seems to be needed for Settings to work, since you target Windows.

              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by
              #6

              @Diracsbracket said in [QML] Percistance of ListModel data:

              did you set all of:
              app.setOrganizationName()
              app.setOrganizationDomain()
              app.setApplicationName()

              No, I will re-try Settings tomorrow morning.
              Thank you very much

              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