Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Problem with Qt5, QML and global variables in javascript file
Forum Updated to NodeBB v4.3 + New Features

Problem with Qt5, QML and global variables in javascript file

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 4 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dario.costa
    wrote on 11 Feb 2014, 09:01 last edited by
    #1

    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

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on 11 Feb 2014, 09:18 last edited by
      #2

      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?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dario.costa
        wrote on 11 Feb 2014, 09:21 last edited by
        #3

        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

        1 Reply Last reply
        0
        • O Offline
          O Offline
          onek24
          wrote on 11 Feb 2014, 09:24 last edited by
          #4

          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?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dario.costa
            wrote on 11 Feb 2014, 09:44 last edited by
            #5

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

            1 Reply Last reply
            0
            • O Offline
              O Offline
              onek24
              wrote on 11 Feb 2014, 10:18 last edited by
              #6

              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?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dario.costa
                wrote on 11 Feb 2014, 10:51 last edited by
                #7

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

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  chrisadams
                  wrote on 14 Feb 2014, 10:34 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    roymuste
                    wrote on 31 Jul 2014, 09:37 last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      roymuste
                      wrote on 1 Aug 2014, 06:49 last edited by
                      #10

                      Any Qt5.2 Defect recorded to this issue?

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        roymuste
                        wrote on 19 Aug 2014, 08:21 last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved