[SOLVED] How to read variables from another function



  • Hi all,

    I have 2 javascript functions in 1 file and I call them from QML file.

    @
    function load() {
    var euro = xml.get(0).lastRate;
    console.log(euro); // returns 25.4750
    }

    function read() {
    console.log(euro); //returns undefined
    }
    @

    I call them here:
    @
    import QtQuick 1.0
    import "XML.js" as Xmlhttp

    ...
    XmlListModel {
    id: xml
    source: "http://www.myxmlfile.xml"
    query: "/CurrencyList"

        XmlRole { name: "lastRate";  query: "Currency[1]/Rate/LastRate/string()"}
    
        onStatusChanged: {
            if (xml.status == 1) {
                Xmlhttp.load();
                Xmlhttp.read();
    
            }
    
        }
    
    }
    

    ...
    @

    Is it possible to read in read() function euro variable?



  • I very much doubt this. I am not a javascript guru but I believe scoping rules limit the euro variable to the scope in which it is declared. In this case that is function load().

    You should move the declaration out of the function if you want to access it.



  • I think the proper way to do this would be like this:

    [code]
    function load() {
    var euro = xml.get(0).lastRate;
    console.log(euro); // returns 25.4750
    return euro;
    }

    function read(var euro) {
    console.log(euro); //returns undefined
    }
    [/code]

    And then you can call them like so in the onStatusChanged:

    [code]
    var euro = Xmlhttp.load();
    Xmlhttp.read(euro);
    [/code]



  • It's working! Thank you very much.


Log in to reply
 

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