[Interest] Change QML Application Style on Runtime

Michael Jackson mike.jackson at bluequartz.net
Wed May 25 18:13:53 CEST 2022


Here is a bit of the code that we used in our desktop QML application to reload the application when the user pressed F7.

    QQmlApplicationEngine engine;


    QmlReloader reloader(&engine);
    QObject::connect(
        &engine, &QQmlApplicationEngine::objectCreated, qApp,
        [](QObject* obj, const QUrl& objUrl) {
          if(obj == nullptr)
          {
            qDebug() << "Can't load " << objUrl << " exiting.";
            QCoreApplication::exit(-1);
          }
        },
        Qt::QueuedConnection);

    qmlRegisterSingletonInstance<IPresentationManager>("NX.interfaces", 1, 0, "Presenter", &presenter);
    qmlRegisterSingletonInstance<NXClipboard>("NX.interfaces", 1, 0, "Clipboard", &clipboard);
    engine.addImportPath("qrc:/");
    engine.addImportPath(DREAM3DNX::k_QmlNXResourceRoot);
    engine.load("qrc:/main.qml");
    qDebug() << "QML2_IMPORT_PATH:" << engine.importPathList();

    QObject::connect(&reloader, &QmlReloader::reload, qApp, [&presenter, &clipboard](QQmlApplicationEngine* engine) {
      qDebug() << "F7 Reloader Activated... ";
      NX::NXParameterManager* parameterManager = NX::NXParameterManager::Instance();
      parameterManager->setQmlParameterPrefix(
#ifdef Q_OS_WINDOWS
          "file:///"
#else
          "file://"
#endif
          + DREAM3DNX::k_QmlNXResourceRoot.toStdString());
      qmlRegisterSingletonInstance<IPresentationManager>("NX.interfaces", 1, 0, "Presenter", &presenter);
      qmlRegisterSingletonInstance<NXClipboard>("NX.interfaces", 1, 0, "Clipboard", &clipboard);
      engine->addImportPath(DREAM3DNX::k_Qml3rdPartyResourceRoot);
      engine->addImportPath(DREAM3DNX::k_QmlNXResourceRoot);
      engine->load(QUrl::fromLocalFile(DREAM3DNX::k_QmlApplicationsResourceRoot + "/DREAM3D/main.qml"));
    });

Maybe that helps. Maybe it does not.

--
Michael Jackson | Owner, President
      BlueQuartz Software
[e] mike.jackson at bluequartz.net
[w] www.bluequartz.net
 

On 5/24/22, 8:31 PM, "Interest on behalf of Alexander Dyagilev" <interest-bounces at qt-project.org on behalf of alervdvcw at gmail.com> wrote:

    Hello,

    Try to call App.changeStyle via queued connection. I.e. your 
    Button::onClicked handler must exit BEFORE you shut the engine down.


    On 5/24/2022 6:45 PM, randomslap--- via Interest wrote:
    > Hello Qt people!
    >
    >     I would like to inquire your assistance regarding this:
    > Is there a way to change the style of a QML(QuickControls2) UI of an 
    > application while it runs? Specifically the program should work with 
    > QQmlApplicationEngine and an ApplicationWindow QML Item.
    >
    >     I have tried making a simple program that could reload its quick 
    > style upon clicking one of the buttons on it. The program appears at 
    > first and seems to close and execute the function that tries to reload 
    > the style but seems to fail on engine.load(). I have sent an image of 
    > all the code and the code files too.
    >
    >     I would specifically like to implement something like this on a 
    > Desktop Environment and being able to load a new style on runtime 
    > would allow for unlimited "theming" options.
    >
    > Thank you for maintaining the Qt Project.
    >
    > _______________________________________________
    > Interest mailing list
    > Interest at qt-project.org
    > https://lists.qt-project.org/listinfo/interest
    _______________________________________________
    Interest mailing list
    Interest at qt-project.org
    https://lists.qt-project.org/listinfo/interest




More information about the Interest mailing list