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



  • I havn't worked with QT < 5.0 yet so i might don't know the changes, but would you mind posting the line in the global.js you are trying to access and the line in qml from where you are trying to access it, so i can try to help you?



  • 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



  • You're welcome. Could you try to set your var wifi_data_ready in your GlobalJS to anything else than 0 and post the console output?



  • It's the same.
    I set wifi_data_ready to 1, but the console log is always TypeError: Cannot read property '0' of undefined



  • Okay. I would say that it can't find your GlobalJS. Could you please create a resource-file and add your GlobalJS to it? I can't import qml files into another qml file until these are in a resource file. Maybe thats also your problem?



  • 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.



  • Any Qt5.2 Defect recorded to this issue?



  • Eventually, Loader help me fix the issue, but I still don't understand why the JS doesn't work for Item in delegate.
    It seems more like a BUG.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.