Search code examples
androidc++qtqtquickcontrols2

Loading next Screen/ ApplicationWindow from Qt quick control 2


I am very new to the Qt quick control 2 development. I am developing one cross platform application for android. I am loading new screen from c++ code as bellow

int main(int argc, char *argv[])

{

QApplication app(argc, argv);

QQmlApplicationEngine engine;

CommunicatorClass mCommunication;

engine.rootContext()->setContextProperty("CommunicatorClass", &mCommunication);

engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

QObject *topLevel = engine.rootObjects().value(0);

QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);

window->show();

return app.exec();

}

This loads my Sign In screen. Once user submits Username,Password application is verifying same in c++ code. Hence communication between qml to c++ is working fine. Now i want to load next screen when username and password gets validated. please guide me how to proceed in this case as i am very new to both c++ and qt quick control 2


Solution

  • One option is to have a property on the object exported from C++ that refers to the "current screen". The code in main.qml can then use a Loader to load that screen.

    That will look a bit like this in C++:

    class CommunicatorClass : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString currentScreen READ currentScreen NOTIFY currentScreenChanged);
    
    public:
        QString currentScreen() const;
    signals:
        void currentScreenChanged();
    };
    

    In QML somewhat like this:

    Window {
        Loader {
            source: CommunicatorClass.currentScreen
        }
    }
    

    assuming that the currentScreen property refers to a QML file relative to main.qml