[QML] Persistance of ListModel data
-
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 }
-
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 -
@LeLev
Either you move the declaration of theListModel
to a persistent part of your app,
or you can save the data in theSettings
, 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 -
Finally i just followed @Diracsbracket suggestion and moved the model to a persistent part of my application. Thx @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 forSettings
to work, since you target Windows. -
@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