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. Javascript trans data to qml have problems

Javascript trans data to qml have problems

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 964 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.
  • S Offline
    S Offline
    skyitachi
    wrote on last edited by
    #1

    ./
    main.qml
    Test.qml
    In Test.qml
    @import QtQuick 2.0
    import QtQuick.LocalStorage 2.0 as Sql
    QtObject{
    id:weatherInfo
    property var db: null
    property var result: []
    function getCurWeatherInfo(cityname)
    {
    var doc = new XMLHttpRequest();
    //a function to be called automatically when the readyState changed
    var cityID = getCityIDs(cityname);
    //result=new Array;
    doc.open("GET", qsTr("xxxxx.html").arg(cityID));
    //weatherArray[0] = new Array;
    doc.onreadystatechange = function() {
    if (doc.readyState == XMLHttpRequest.DONE) {
    var jsonObject = eval('(' + doc.responseText + ')');
    result['temp']=jsonObject['weatherinfo']['temp']
    result['city']=jsonObject['weatherinfo']['city']
    console.log(result['temp']);
    console.log(result['city']);
    }
    }
    doc.send();
    }
    }
    }
    @

    In main.qml
    @
    Rectangle{
    id:rect
    Test{
    id:testItem
    }
    Button{
    id:someButton
    onClicked:{
    testItem. getCurWeatherInfo(cityname)
    console.log(testItem.result['temp'] + " "+ result['city'])
    }
    }
    }
    @
    my quesion is that :
    why in main.qml output:
    undefined undefined
    in Test.qml output:
    27 city

    it is very confusing , I have set result property in Test.qml, it should be a global var in Test.qml but why in func getCurWeatherInfo() it cannot be updated.

    PS: it is a trick about javascript asynchronous function???
    I need everyone's help, thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      skyitachi
      wrote on last edited by
      #2

      I use a way to solve this problem, use result as a parameter in getCurweatherInfo(cityName, result),it can be ok.
      PS: it seemed very silly,because I am a javascript and qml freshman.

      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