Problem with Qt5, QML and global variables in javascript file
-
Hi all,
I'm doing porting of an application from Qt4.8 to Qt5, but I have a problem with the import of a javascript file in QML files.I have a main QML file with the line 'import "global.js" as GlobalJS' at the top, and in this file I can correctly access all the variables I stored inside (in the javascript file I added .pragma library at the top).
Inside this main qml file I have some loader that open other QML files on needed, and also in these files I have the same import line at the top.
When one of these page is loaded, when I try to access GlobalJS variables, I have an error on the console like:
qrc:///qml/Test_QT5/test.qml:11: TypeError: Cannot read property '0' of undefined.With Qt4.8, this application is working correctly... why with Qt5 I cannot access to the GlobalJS from pages other than the main??
Thanks a lot for the help
Dario -
Thanks for the reply.
This is the line in qml file:GlobalJS.wifi_data_ready = 0;
And this id the declaration of this variable in javascript file:
var wifi_data_ready = 0;
Thanks a lot
Dario -
It's the same.
I set wifi_data_ready to 1, but the console log is always TypeError: Cannot read property '0' of undefined -
Nothing... also if I put the global.js file inside the resources.qrc where the qml files are, the result is always the same.
If I change the import path (just to try to see if my js file was found correctly), the console log tell me that the file is not found, when I correct the path the error come back to the TypeError, so I'm sure that the file is correctly found.I don't understand why from the main qml file I have no problem to access variables....
-
Which version of Qt 5 are you using? Qt 5.2? This sounds like a regression bug, to me. Please file a bug report on the Qt Project bug tracker including a minimal example which shows the problem.
Thanks,
Chris. -
I also met same problem, If the JS var/function is called in the top level/main qml, it works, but if I invoke the JS var/function in the sub-level qml (e.g. a delegate.qml) , it always complaint "TypeError: Cannot read property '0' of undefined", no matter it is a function or variable in JS;
I'm using Qt5.2.1.