Please nominate your Qt Champions for 2021!

[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: ""+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

  • 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: ""
    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":

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

    @XmlListModel {

    source: "";
    query: "/html/head"
    namespaceDeclarations: "declare default element namespace '';"
    XmlRole { name: "image"; query: "title/string()" }
    onStatusChanged: {
        if (status == XmlListModel.Ready)
            titleText.text = get(0).image;


    Text {
    id: titleText

Log in to reply