Unsolved Hot reloading QML with QRC file.
-
Hi all,
I've spend 2hours trying to reload my QML within my running application.. with no luck.!
I just want to have a hot key so that I can decide when developing when to reload everything.
I found a few ways of doing it (here and here ) but nothing worked properly, I manage to get some components being reloaded on the fly properly but not everything...I nailed it down to a small failure case.
I'm using a qrc file to manage all my qml. and ressource in my qml.qrc are handled as follow:<RCC> <qresource prefix="/"> <file alias="main.qml">./main.qml</file> </qresource> <qresource prefix="/Bar"> <file alias="Foo.qml">./Foo.qml</file> </qresource> </RCC>
My main.qml looks like this
import "qrc:/Bar" ApplicationWindow { id: root visible: true width: 1920 height: 1080 Foo { anchors.fill: parent } }
My main.cpp
int main(int argc, char *argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; App _app; engine.rootContext()->setContextProperty(QStringLiteral("app"), &_app); _app.set_engine(&engine); QUrl source (QStringLiteral("qrc:/main.qml")); engine.load(source); if (engine.rootObjects().isEmpty()) return -1; return app.exec();
My app class which is responsible for clearing cache and reloading qml..
class App : public QObject { Q_OBJECT public: App( QObject* i_parent = nullptr ); ~App(); Q_INVOKABLE void load_qml(); void set_engine( QQmlApplicationEngine* i_engine ){ m_engine = i_engine; } private: QQmlApplicationEngine* m_engine; QString m_qmlqrc_file; };
and app.cpp
#include "app.hpp" #include <QFileInfo> #include <QFileSelector> App::App( QObject* i_parent ) : QObject( i_parent ) { m_qmlqrc_file = QString(QRC_SOURCE_PATH) + "qml.qrc"; } App::~App(){} void App::load_qml() { std::cout << " here load qml " << std::endl; m_engine->clearComponentCache(); std::cout << " m engine cleaned " << std::endl; std::cout << " loading " << QFileInfo(m_qmlqrc_file).absolutePath().toStdString() << "/main.qml" << std::endl; QFileSelector selector; // Q_CLEANUP_RESOURCE(qml); // Q_INIT_RESOURCE(qml); QUrl source ( selector.select( QFileInfo(m_qmlqrc_file).absolutePath() + "/main.qml" ) ); // QUrl source ( QFileInfo("qrc:/main.qml").absoluteFilePath()); m_engine->load(source); }
Only the things that are at the root of qrc are being reloaded correctly at runtime (here only main.qml is reloaded, changing something in Foo.qml has no effect).
But if I change my qml file and qml.qrc to look like this :<RCC> <qresource prefix="/"> <file alias="main.qml">./main.qml</file> <file alias="Foo.qml">./Foo.qml</file> </qresource> </RCC>
Then the changes in Foo.qml are picked up at runtime.
I don't understand what is happening and how can I get qml to reload while running the app but still keeping my qml.qrc (and files) organized as before. Obviously my project is bigger than that and having all my qml files in one place is not an option.Any help would be greatly appreciated !
-
-
Thanks, I will give it a try but I would rather not use another tool if I can.
-
After trying qmllive doesn't seem to handle properly my .qrc file..
-
@EmilieG you're doing something strange, why are you importing bar this way
import "qrc:/Bar"
instead of
import "Bar"
that removes the explicit dependency to the qrc file and should help
-
@EmilieG said in Hot reloading QML with QRC file.:
After trying qmllive doesn't seem to handle properly my .qrc file..
As far as I know, live update only works for QML outside from resource file.
If QML is in resource file, it is "frozen". You have to load QML from file system.