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. [SOLVED] How to read variables from another function
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to read variables from another function

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 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.
  • M Offline
    M Offline
    michprev
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hornsj2
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pennomi
        wrote on last edited by
        #3

        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]

        1 Reply Last reply
        0
        • M Offline
          M Offline
          michprev
          wrote on last edited by
          #4

          It's working! Thank you very much.

          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