I have an XML file to show list of items(listview), on click of each item i am switching to page which reads the XMLListmodel for each category using pagestack.push.
After pressing back and again going to same page results in lost of ListModel data.
How to modularize the code into several QML files without loosing the ListModel data information.
Please let me know.
Attaching the sample snippet.
main.qml
if(currentPageName == "menuName")
{
PageStack.push(Qt.resolvedUrl("showChosenList.qml"));
}
showChosenList.qml
ListModel{
id: hotelMainMenuModel
}
XmlListModel {
id: hotelMainMenuFetch
source: "hotelMenu.xml"
query: "/hotelMenu/menuCategories/categoryList/mainMenu"
onStatusChanged: {
if (status === XmlListModel.Ready) {
for (var i = 0; i < count; i++)
{
hotelMainMenuModel.append({"name": get(i).name, "displayText": get(i).name, "pageName": get(i).pageName})
}
}
}
XmlRole { name: "name"; query: "name/string()" }
XmlRole { name: "pageName"; query: "pageName/string()" }
}
}
Best Answer
I believe that your problem is that pushing to the stack a filename creates a new object each time, and this new object doesn't share data with any of the previously created objects. Instead, make an instance of the page you want to push in your top level QML file and push it to the page stack each time you want it shown.
Here's an example that I hope illustrates what's going on.
main.qml
SubPage.qml
Note that the counter on the page loaded from the object persists as you move back and forth in the page stack, while the page loaded from the file has the counter set to 0 each time it is loaded. Additionally, the latter logs a completion event to the console each time, while the former logs this only when the program starts.