Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Multi-page QML application



  • G'day,

    I'm looking to develop a multi-page application utilising many discrete .qml files. Each .qml file represents a different page (e.g. settings page, help page, etc.). The user can set a default page to be loaded on application start, so there isn't a 'main.qml' equivalent. Below is a simplistic representation of what I'm trying to accomplish. From the image, any of the QML files (QML 1 through QML 5) can be shown on application start, with each having its own set of configuration files. Once in a page, the user can navigate to any of the other .qml pages. The pages DO NOT have to be loaded into memory on application start, they can be instantiated during runtime.

    0_1564558786640_Capture.PNG

    As for the startup functionality, I have a preference parameter which saves the user's preference for their startup page. In main.cpp, I then specify which page is loaded into the QQmlApplicationEngine as follows (number of available QML files reduced to 2 for simplicity in the example code):

    //Main.cpp
    QQmlApplicationEngine engine;
    
    preferencesFile *preferencesFilePointer = preferencesFile::getInstance();
    
    if (preferencesFilePointer->startupQML() == "QML 1"){
        engine.load(QUrl(QStringLiteral("qrc:/QML1.qml")));
    } else {
        engine.load(QUrl(QStringLiteral("qrc:/QML2.qml")));
    }
    

    The above code works as expected; if the user has saved QML1.qml as their startup preference, the QML1.qml file is presented. What I'm now trying to understand is the following:

    1. Is the above a reasonable approach?
    2. If the answer to 1 above is yes, would Loaders be the optimal/preferred mechanism by which I can switch between QML files in runtime?

    I'm sure I'll have follow up questions, but some guidance on the above 2 would be greatly appreciated for now!



  • @jars121 said in Multi-page QML application:

    Is the above a reasonable approach?

    Yep, it looks fine, why not?

    If the answer to 1 above is yes, would Loaders be the optimal/preferred mechanism by which I can switch between QML files in runtime?

    Loaders are fine too. You also may look into StackView or even go in dynamic object creation by hands.



  • hi

    I wonder how you do this, is the root component of all QMLs (1,2,3.. ) a QML ApplicationWindow or QML Window ?

    @jars121 said in Multi-page QML application:

    Is the above a reasonable approach?

    a simpler approach is to actually have a main.qml and have a Loader in it to load the page + Settings to save your prefered page

    //main.qml
    ApplicationWindow {
    property string initialPage : "QML1"

    Settings {
        property alias firstPage : initialPage  
    }
    

    Loader{
    // set the first page
    }

    }



  • Thank you both for your input.

    @LeLev each QML is based on a Window architecture.

    I gave this some further thought over night, and I tend to agree that using a main.qml is still probably the best approach. I'll eventually look to incorporate a SwipeView for movement between these various pages, so having a main.qml act as a loading page/container would be ideal.

    I'll have a play with this approach today and post back with any issues.


Log in to reply