Ubuntu – Listmodel in QML: How to retain Listmodel values on page switch in page stack

application-developmentqml

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

import QtQuick 2.0
import Ubuntu.Components 0.1

MainView {
    width: units.gu(50)
    height: units.gu(75)

    PageStack {
        id: pageStack

        Page {
            id: page
            title: "Top Page"
            visible: false

            Column {
                anchors.fill: parent
                Button {
                    width: parent.width
                    text: "Open page from object"
                    onClicked: pageStack.push(subPage)
                }

                Button {
                    width: parent.width
                    text: "Open page from file"
                    onClicked: pageStack.push(Qt.resolvedUrl("SubPage.qml"))
                }
            }
        }

        SubPage {
            // This is an instance of the object declared in SubPage.qml.  All you need
            // to do to make this work is have SubPage.qml in the same directory as
            // this QML file.
            id: subPage
            visible: false
        }

        Component.onCompleted: pageStack.push(page)
    }
}

SubPage.qml

import QtQuick 2.0
import Ubuntu.Components 0.1

Page {
    title: "SubPage"

    Component.onCompleted: console.log("Made a new page")

    Button {
        width: parent.width
        property int count: 0
        text: "Clicked %1 times".arg(count)
        onClicked: count += 1
    }
}

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.

Related Question