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



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

  • Moderators

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

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



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

  • Moderators

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



  • @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?


  • Moderators

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



  • @raven-worx I will try, thanks


Log in to reply
 

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