[Moved] retrieve variable from XML and show in text field



  • Hi
    I need some help with Qt and XML. I retrieve the latitude from google API using
    @XmlListModel {
    // property int index
    id: latModel
    source: "http://maps.googleapis.com/maps/api/geocode/xml?address="+city+"&sensor=false"
    query: "/GeocodeResponse/result"
    XmlRole { name: "lat"; query: "geometry/location/lat/string()" }
    XmlRole { name: "lng"; query: "geometry/location/lng/number()" }
    XmlRole { name: "addr"; query: "formatted_address/string()" }
    }@
    However when I use it to retrieve the 'lat' variable to put in text, I get 'ReferenceError: Can't find variable: lat'. I am able to use the same variable in a list view without any problem. Pl let me know how to retrive the variable 'lat' and display in the text field.
    @Text {
    id: lattxt
    x: 10
    y: 200
    width: 100
    height: 30
    color: "#360414"
    text: lat
    font.bold: true
    font.pixelSize: 40
    }
    @
    THANKS IN ADVANCE



  • You can not use XmlListModel in this way. A model is supposed to be used with a view, and the XmlRoles will be made available to delegates in that view. Outside of that scope, it is meaningless.



  • Try putting your text item in a listview as a delegate that changes the text per the docs:

    @
    XmlListModel {
    id: xmlModel
    source: "http://www.mysite.com/feed.xml"
    query: "/rss/channel/item"

     XmlRole { name: "title"; query: "title/string()" }
     XmlRole { name: "pubDate"; query: "pubDate/string()" }
    

    }
    @

    @
    ListView {
    width: 180; height: 300
    model: xmlModel
    delegate: Text { text: title + ": " + pubDate }
    }
    @

    Of course you use your text and xml role in the example.

    The example for the QML Twitter project is a good illustration as to how XML Roles get displayed.



  • Thanks for the reply. I will look into it. Sorry I didn't notice the QML thread so I posted my question under Qt.



  • "LINK":http://permalink.gmane.org/gmane.comp.lib.qt.qml/555

    Basically you can do as advised though this is far from optimal...

    @XmlListModel {

    source: "http://xkcd.com/100/";
    query: "/html/head"
    namespaceDeclarations: "declare default element namespace 'http://www.w3.org/1999/xhtml';"
    
    XmlRole { name: "image"; query: "title/string()" }
    
    onStatusChanged: {
        if (status == XmlListModel.Ready)
            titleText.text = get(0).image;
    }
    

    }

    Text {
    id: titleText
    }@


Log in to reply
 

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