[solved] Reload a JS file at runtime (I'm testing ClojureScript as a workflow)
-
wrote on 16 Apr 2014, 05:21 last edited by
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.
-
wrote on 6 May 2014, 06:57 last edited by
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. -
wrote on 6 May 2014, 07:00 last edited by
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.
-
wrote on 27 Jun 2014, 02:29 last edited by
hello, qttester5 you said "I did some extensive experiments yesterday and arrived at the same conclusion." Could you tell me the another method which can get the same result?
-
wrote on 27 Jun 2014, 04:36 last edited by
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.
-
wrote on 29 Jun 2014, 19:32 last edited by
[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?