[solved] Reload a JS file at runtime (I'm testing ClojureScript as a workflow)
-
I am wondering if it is possible for a QML file to re-import a JS file while the Qt app is running? For example, the app is running, but you have an external JS editor that you are working in and when you save changes to a JS file, you want the QML to be able to recognize it without having to restart the entire Qt app. The standard "import" process at the top of a QML file would probably not be the best place for something like this. The best I can think of is to have the QML read a JS file dynamically and then call "eval()" on it. Thoughts?
For anyone curious, I'm interested in have a dynamic development experience in Qt using ClojureScript outside of Qt to write the code that becomes JS, and then QML uses this JS. The translation to JS is certainly easy and works, but it's the constant lispy development workflow that I'm trying to emulate. Qt and QML are a great set of UI tools and it would be fun to access them in real-time through ClojureScript.
-
Right, you won't be able to use the import syntax, as the engine will parse and compile the import at import time, and cache the result for later use. Attempting to "re-import" it (via a dynamic Qt.createQmlObject() or similar which includes an import) will result in the cached compiled data being used.
You will, however, be able to use Qt.include().
So, probably the best way is to have a shim .js resource which has: "function reload() { Qt.include(fileName); /* now set global var values etc to whatever they need to be, based on the reloaded code which was included in this scope */ }"I haven't tested this personally, but cannot see why it wouldn't work.
Cheers,
Chris. -
Note that there is a QQmlEngine::clearComponentCache() (or something) function. I'm fairly certain that that will only clear typedata for dynamically loaded QML types, not compiled JS units, but I may be wrong.
-
[quote author="qttester5" date="1399359647"]Yes, that does work. I should have updated this question to note it was solved. I did some extensive experiments yesterday and arrived at the same conclusion. Thanks.[/quote]
This is very interesting.
Could you perhaps give some more detail about how your clojurescript -> qml setup works? Does this mean you can have arbitrary clojurescript generated javascript executed by the Qt V4VM js engine?