[Solved] How to switch from page1.qml (on image click) by sending a string to page2.qml and vice-a-versa?



  • Hi,

    I've 2 qml files namely page1.qml and page2.qml.

    page1.qml is my main qml file which is getting loaded having got the command "viewer.setMainQmlFile(QStringLiteral("qml/QmlQml/page1.qml"));"

    I wish to show page2.qml on image click of page1.qml by sending a string. The received string by page2.qml is shown there and on some other event of page2, page1 gets displayed.

    Quick help from some source is awaited.

    Kindly guide me how to proceed.



  • You need to use a Loader and Connections.

    @Item {
    id: mainItem
    width: 600
    height: 400
    Component.onCompleted: pageLoader.setSource("Page1.qml")

            signal loadPage(string pageName)
    
            Loader {
                id: pageLoader
    
                anchors.fill: parent
            }
    
            Connections {
                id: pageConnections
                ignoreUnknownSignals: true
                target: pageLoader.item
    
                onLoadPage: {
                    pageLoader.source = pageName;
                }
            }
    

    }@

    Try something like this. I don't have time to test this but Connections and Loader are what you'll need.



  • [Solved] Thanks. I've applied the above mentioned Loader, Connections and could successfully switch to second qml from first qml and vice-a-versa. I'm yet to try out sending String from second and receiving in first.

    In fact, I wish to send and receive an object of Person class between 2 or multiple qml pages (conditionally). Any inputs on this please?



  • For sending strings, you can check the code posted above. When you want to switch pages, simply send the loadPage signal and the string as the argument. As you can see, I've used the pageName string to change the page.

    I don't know about the Person class, maybe you can use variant, or perhaps QtObject, but I'm really not sure.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.