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. How to show data dynamically in qml

How to show data dynamically in qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 910 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.
  • T Offline
    T Offline
    trupti007
    wrote on last edited by
    #1
    This post is deleted!
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      It all depends on what you prefer and how complicated the data is. Here are some possibilities:

      • generate model using ListModel
      • push data to C++ and use a subclass of QAbstractItemModel
      • if the data is a simple list of strings, you can display it directly (just set it as model in ListView or something)

      A full list is in the docs: https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html#models

      (Z(:^

      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        If you fetch external data in JSON, I would use JsonListModel from https://github.com/benlau/qsyncable
        It acts as a proper QAbstractItemModel and make a diff of the changes between update to send the correct signals (dataChanged, rowsInserted,rowsRemoved, ...)

        Something like so:

        // asuming the JSON response is [{"id": 1, "foo":  "xxx", "bar": 42}.  {"id": 2, "foo":  "yyy", "bar": 43}]
        JsonListModel {
            id: jsonListModel
            keyField: "id"
        }
        
        ListView {
            anchors.fill: parent
            model: jsonListModel
            delegate: ItemDelegate { text: model.foo + " - " + model.bar  }
        }
        
        //...
        Timer {
             running: true
             interval: 5000
             triggeredOnStart: true
             repeat: true
             onTriggered: {
                 var xhr = new XMLHttpRequest();
                 xhr.onreadystatechange = function() {
                     if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200)
                         jsonListModel.source = JSON.parse(xhr.responseText);
                 };
                 xhr.open("GET", yourUrl);
                 xhr.send();
             }
        }
        

        If you only fetch your data once and don't need to update it, you can just assign the result of JSON.parse to your ListView (or Repeater/MapItemView/Instantiator/...).

        1 Reply Last reply
        2

        • Login

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