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

Creating a start dialog to set options which functionalities to load - second QQmlApplicationEngine?



  • Hello,

    I have a program which can access hardware or can be used as a file reader without hardware.

    I would like to show the user a QML dialog at software start to select whether the software shall run in hardware access mode or file reader mode. And then afterwards I would like to create instances of the necessary classes and set the right context properties in my main.cpp.

    How can I achieve this?
    I thought about creating two QQmlApplication engines.
    The first one asks the user, sets a variable which mode shall be used and is then closed.
    Then the necessary classes would be instantiated, registered and the main QQmlApplicationEngine would load.

    Is there another good way?
    If I would try it like described, my question is, how can I make my Code in my main.cpp wait until the first engine is closed (engine.load() is not blocking)?

    Thank you very much :-)



  • @robro

    Would something like the following work:

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QQmlApplicationEngine* engine;
    
        engine = new QQmlApplicationEngine();
        //setup engine for app1
        //...
        //Setup C++ to get values back from engine
        //...
        engine->load(QUrl(QStringLiteral("qrc:/main1.qml")));
    
        if (engine->rootObjects().isEmpty())
            return -1;
    
        int a = app.exec();
        qDebug() << "Ended App 1 with result: " << a;
        delete(engine);
    
        engine = new QQmlApplicationEngine();
        //Setup engine for app2
        //...
        engine->load(QUrl(QStringLiteral("qrc:/main2.qml")));
    
        if (engine->rootObjects().isEmpty())
            return -1;
    
        int b = app.exec();
        qDebug() << "Ended App 2 with result: " << b;
    
        delete(engine);
        return b;
    }
    


  • Thank you very much!


Log in to reply