El_Mewo
El_Mewo

Reputation: 109

Is there a way to hot reload QML when using the new CMake API for modules?

I have seen many outdated answers about this question, but they do not take the new CMake QML Modules API into consideration. In short, you declare a qml module in CMake with some QML and/or C++ or asset files and you get a generated plugin to be linked against by your application and a generated qmldir file in the build directory. All files are added to the resource system. I already tried a shortcut to reload the qml in the engine using QQmlApplication::loadFromModule("Main", "Main") (see link, this is new in 6.5 and does not seem to rely on qrc only).

Is there any chance to hot reload QML files within a running application with the given setup?

Upvotes: 2

Views: 666

Answers (2)

Ave Milia
Ave Milia

Reputation: 649

To make this work, you need to do the following changes:

  1. Following your example of QQmlApplication::loadFromModule usage, create a Main directory and put a Main.qml file into it.
  2. Create a Main/qmldir file with the contents like
module Main
Main 1.0 Main.qml

You can also copy the qmldir file from the build directory, where the files generated by qt_add_qml_module are located.

  1. In C++ code, add a call to QQmlEngine::addImportPath("/path/to/parent/dir/of/Main"), before calling QQmlApplication::loadFromModule("Main", "Main").

As long as you add your module to the import path (even if it's conditionally decided at runtime), the loadFromModule function will prefer to load Main.qml from the disk, and not via .qrc.

Upvotes: 0

DaLexto
DaLexto

Reputation: 1

Yes, I recommend QmlPreview:

Build->QmlPreview

Upvotes: -2

Related Questions