Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. What is the best way to declare a global variable in QML?

What is the best way to declare a global variable in QML?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
7 Posts 2 Posters 14.2k 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.
  • N Offline
    N Offline
    NTMS
    wrote on last edited by
    #1

    Hi,

    I have a 2 (*.qml) as shown below. In my QML Application, I'd like to execute a Javascript-code before loading it. Then I need a global variable like "var result" so I can access in other qml page (Currency.qml). Component.onCompleted is not what I am looking for. I need something before loading the Main.qml.

    How do I do that? Thanks.

    // Startup qml
    Main.qml
    {
             // js function load json data
              ....
              ....
             var result = JSON.parse(request.responseText)
    
         // Load Currency qml
          Currency.qml {
    
          }
    }
    
    
    Currency.qml
    {
            // assign a value to stg textbox in Currency.qml
             eStgSell.text = result.stg.sell
    }
    
    raven-worxR 1 Reply Last reply
    0
    • N NTMS

      Hi,

      I have a 2 (*.qml) as shown below. In my QML Application, I'd like to execute a Javascript-code before loading it. Then I need a global variable like "var result" so I can access in other qml page (Currency.qml). Component.onCompleted is not what I am looking for. I need something before loading the Main.qml.

      How do I do that? Thanks.

      // Startup qml
      Main.qml
      {
               // js function load json data
                ....
                ....
               var result = JSON.parse(request.responseText)
      
           // Load Currency qml
            Currency.qml {
      
            }
      }
      
      
      Currency.qml
      {
              // assign a value to stg textbox in Currency.qml
               eStgSell.text = result.stg.sell
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @NTMS
      Either a QML singleton object/component holding the variable.

      Or even easier use a context property (C++).

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • N Offline
        N Offline
        NTMS
        wrote on last edited by
        #3

        @raven-worx I am getting error when I use singleton

        I created a GlobalStyleMain.qml as shown below.

        pragma Singleton
        import VPlayApps 1.0
        import QtQuick 2.0
        
        QtObject {
            property var currencyGetJsonData: ({})
            property var weatherGetJsonData: ({})
        }
        

        In Main.qml I call getCurrencyJsonData as shown below

        import "."
        
        ....
        ....
        ....
        
           function getCurrencyJsonData() {
               var request = new XMLHttpRequest()
               request.open('GET', 'https://****************', true);
               request.onreadystatechange = function() {
                   if (request.readyState === XMLHttpRequest.DONE) {
                       if (request.status && request.status === 200) {
                           
                          // Json data assign to global varible in here
                           GlobalStyleMain.currencyGetJsonData  = JSON.parse(request.responseText)
                           console.log("Success")
                       }
                       else {
                           console.log("LogError:", request.status, request.statusText)
                       }
                   }
               }
               request.send()
               console.log("Json Finished")
           }
        

        In Currency.qml I assign the values as shown below (with error)

        import "."
        
        ....
        ....
        ....
        
        // I get error in below as:
        // TypeError: Cannot read property 'stg' of undefined
         eStgSell.text = GlobalStyleMain.currencyGetJsonData.stg.sell
        
        raven-worxR 1 Reply Last reply
        0
        • N NTMS

          @raven-worx I am getting error when I use singleton

          I created a GlobalStyleMain.qml as shown below.

          pragma Singleton
          import VPlayApps 1.0
          import QtQuick 2.0
          
          QtObject {
              property var currencyGetJsonData: ({})
              property var weatherGetJsonData: ({})
          }
          

          In Main.qml I call getCurrencyJsonData as shown below

          import "."
          
          ....
          ....
          ....
          
             function getCurrencyJsonData() {
                 var request = new XMLHttpRequest()
                 request.open('GET', 'https://****************', true);
                 request.onreadystatechange = function() {
                     if (request.readyState === XMLHttpRequest.DONE) {
                         if (request.status && request.status === 200) {
                             
                            // Json data assign to global varible in here
                             GlobalStyleMain.currencyGetJsonData  = JSON.parse(request.responseText)
                             console.log("Success")
                         }
                         else {
                             console.log("LogError:", request.status, request.statusText)
                         }
                     }
                 }
                 request.send()
                 console.log("Json Finished")
             }
          

          In Currency.qml I assign the values as shown below (with error)

          import "."
          
          ....
          ....
          ....
          
          // I get error in below as:
          // TypeError: Cannot read property 'stg' of undefined
           eStgSell.text = GlobalStyleMain.currencyGetJsonData.stg.sell
          
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @NTMS
          did you create a qmldir file as described in the docs?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • N Offline
            N Offline
            NTMS
            wrote on last edited by
            #5

            @raven-worx
            I try to implement http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html
            and there isn't any qmldir files?

            I red too many articles about qml and C++. Any every time I get more confused.
            Sorry for asking but am new to qml :( do you know any easy way out?

            raven-worxR 1 Reply Last reply
            0
            • N NTMS

              @raven-worx
              I try to implement http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html
              and there isn't any qmldir files?

              I red too many articles about qml and C++. Any every time I get more confused.
              Sorry for asking but am new to qml :( do you know any easy way out?

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @NTMS
              you should follow the link i had posted...

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • N Offline
                N Offline
                NTMS
                wrote on last edited by
                #7

                @raven-worx I will try, thanks

                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