Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. JS function returns value but not visible when calling this function from QML
Forum Updated to NodeBB v4.3 + New Features

JS function returns value but not visible when calling this function from QML

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 288 Views
  • 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.
  • S Offline
    S Offline
    shokarta
    wrote on 14 May 2021, 09:24 last edited by
    #1

    Dear all,

    i have this:

    main.qml:

        import 'JavaScript.js' as JS
        property int recordsAmount
        Timer {
            id: readTimer
            interval: 5000
            running: true
            repeat: true
            triggeredOnStart: true
            onTriggered: {
                //recordsAmount = JS.httpRequest("offline_count_record.php");    // this doesnt work because it cant get the value, see next line
                console.log(JS.httpRequest("offline_count_record.php"));    // this debugs "undefined"
            }
        }
    

    JavaScript.js:

    function httpRequest(file) {
        var url = "http://www.someurl.com/" + file;
    
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState === XMLHttpRequest.DONE && xmlhttp.status === 200) {
    
                var arr = JSON.parse(xmlhttp.responseText.toString());
    
                if(arr.result === false) { console.log(arr.error); return false; }
                else {
                     if(file === "offline_count_record.php") {
                         console.log(arr.count); // this debugs correctly the number, also typeof(arr.count) returns "number", so this is correct
                         return arr.count; }
                    else { return true; }
                }
    
            }
        }
        xmlhttp.open("GET", url, false);    // false for sync, true for async
        xmlhttp.send();
    }
    

    example of the "arr" returned by xmlhttp is:

    {"result":true,"count":158}
    

    so how come the timer tick debugs "undefined" ???

    each tick i have in console this:

    qml: 158
    qml: undefined
    

    where first line (158 value) comes from JS script console.log, and second line ("undefined") comes from Timer tick console.log

    J 1 Reply Last reply 14 May 2021, 09:28
    0
    • S shokarta
      14 May 2021, 09:24

      Dear all,

      i have this:

      main.qml:

          import 'JavaScript.js' as JS
          property int recordsAmount
          Timer {
              id: readTimer
              interval: 5000
              running: true
              repeat: true
              triggeredOnStart: true
              onTriggered: {
                  //recordsAmount = JS.httpRequest("offline_count_record.php");    // this doesnt work because it cant get the value, see next line
                  console.log(JS.httpRequest("offline_count_record.php"));    // this debugs "undefined"
              }
          }
      

      JavaScript.js:

      function httpRequest(file) {
          var url = "http://www.someurl.com/" + file;
      
          var xmlhttp = new XMLHttpRequest();
          xmlhttp.onreadystatechange = function() {
              if (xmlhttp.readyState === XMLHttpRequest.DONE && xmlhttp.status === 200) {
      
                  var arr = JSON.parse(xmlhttp.responseText.toString());
      
                  if(arr.result === false) { console.log(arr.error); return false; }
                  else {
                       if(file === "offline_count_record.php") {
                           console.log(arr.count); // this debugs correctly the number, also typeof(arr.count) returns "number", so this is correct
                           return arr.count; }
                      else { return true; }
                  }
      
              }
          }
          xmlhttp.open("GET", url, false);    // false for sync, true for async
          xmlhttp.send();
      }
      

      example of the "arr" returned by xmlhttp is:

      {"result":true,"count":158}
      

      so how come the timer tick debugs "undefined" ???

      each tick i have in console this:

      qml: 158
      qml: undefined
      

      where first line (158 value) comes from JS script console.log, and second line ("undefined") comes from Timer tick console.log

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 14 May 2021, 09:28 last edited by
      #2

      @shokarta I'm not an JS expert but I can't see where function httpRequest(file) returns anything?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shokarta
        wrote on 14 May 2021, 09:33 last edited by
        #3

        @shokarta said in JS function returns value but not visible when calling this function from QML:

        return arr.count;

        yea just minute ago, i have same thought...
        at the moment im invoking JS.httpRequestA(file) from QML file, so it sends the request...
        however it does not return value at the same time... therefore the result is undefined :(

        so i have to find a way to rewrite the JS function to send the request, and do nothing till it returns its valuer to be returned by this JS function back to QML

        K 1 Reply Last reply 14 May 2021, 09:46
        0
        • S shokarta
          14 May 2021, 09:33

          @shokarta said in JS function returns value but not visible when calling this function from QML:

          return arr.count;

          yea just minute ago, i have same thought...
          at the moment im invoking JS.httpRequestA(file) from QML file, so it sends the request...
          however it does not return value at the same time... therefore the result is undefined :(

          so i have to find a way to rewrite the JS function to send the request, and do nothing till it returns its valuer to be returned by this JS function back to QML

          K Offline
          K Offline
          KroMignon
          wrote on 14 May 2021, 09:46 last edited by
          #4

          @shokarta said in JS function returns value but not visible when calling this function from QML:

          so i have to find a way to rewrite the JS function to send the request, and do nothing till it returns its valuer to be returned by this JS function back to QML

          I would suggest you to take a look at this example ==> https://gist.github.com/40/3192269

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          0

          1/4

          14 May 2021, 09:24

          • Login

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