QML script provider similar to image provider
-
Ok. but how do i make Qt/QML use this code to download the file? Can it be done via a plugin?
I want to be able to use this custom code when someone writes:
import "some-uri" as Remote
At this point it should get the file using my plugin. -
bq. but how do i make Qt/QML use this code to download the file?
From C++ did you check the examples of QNetworkAccessManager::get ?
You can call C++ functions from QML. Check "qtqml-cppintegration-topic":http://qt-project.org/doc/qt-5/qtqml-cppintegration-topic.html and especially "qtqml-cppintegration-exposecppattributes":http://qt-project.org/doc/qt-5/qtqml-cppintegration-exposecppattributes.html
I'm not sure about the plugin as I have never used one. -
@p3c0: Thanks. I get how to use the QNetworkAccessManager to download the file. But I need to be able to run this code whenever someone uses the import statement for external javascript files. Sorry for not being clear before.
-
Well then check "registering-an-instantiable-object-type":http://qt-project.org/doc/qt-5/qtqml-cppintegration-definetypes.html#registering-an-instantiable-object-type. Create a Q_INVOKABLE function in it where you download the file then you can call this function from QML after instantiating it.
-
@p3c0: I am not able to get the example you stated above to work actually
import "http://192.168.0.16/sample.js" as Remote
I have my own node.js file server running on 8030 which i am trying to access using the below import statement:
import "http://127.0.0.1:8030/testbrowser.js" as Browser
Sometimes it does send a request to my server and my server correctly responds too, but the app then exits for some reason. do you know why that is?
thanks,
Rohit -
Sorry but i haven't used node.js. I have used it with particularly simple example like few JavaScript functions in sample.js and it works as expected.
-
Actually node.js is not the issue here..as the server does work when i try to access the file in the browser.
Do you know why the download would not work in QML? My app exits with an exit code 255.
Thanks -
Can you try with some simple Javascript file which will contain just a few functions ?
-
i tried with an empty javascript file too and it did not seem to work
-
I tested it with apache server and it worked. The js file contains a simple function which returns a string.
sample.js
@
function myFunction() {
return "Hello Qt"
}
@and the from QML
@
import "http://192.168.1.3/sample.js" as Remote
...
//and on some Button click calling it as
Remote.myFunction()
@ -
It seems to work when I create a Qt Quick Application project from Qt Creator and try to import the javascript file from the node js server.
However the import fails when I create a Qt Quick UI project. Is there a way to enable this behavior in a Qt Quick UI project?