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. Updating ListView using JS object model
Forum Update on Monday, May 27th 2025

Updating ListView using JS object model

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 537 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.
  • P Offline
    P Offline
    Phrogz
    wrote on 29 Jun 2018, 22:37 last edited by
    #1

    Given a simple QML ListView using an object-based model, is there any way to force updates to the model, that will propagate to changes in the delegates?

    For example, let's say I have a list with model like below, and I want the ct property to increment when I click on a button, and have that reflected in the button label:

    ListView {
        id: mylist
        model: [ {n:"a", ct:0}, {n:"b", ct:0} ]
        delegate: Button {
            text: modelData.n + modelData.ct
            onClicked: {
                modelData.ct++;
                console.log(JSON.stringify(mylist.model))
            }
        }
    }
    

    The code above constantly outputs the original model (where ct is 0) on each click. Same if I attempt to force a change like: mylist.model[index].ct += 1.

    How can I change the model (without using a ListModel, which does not support nested data structures I am currently using)?

    O 1 Reply Last reply 29 Jun 2018, 23:35
    0
    • P Phrogz
      29 Jun 2018, 22:37

      Given a simple QML ListView using an object-based model, is there any way to force updates to the model, that will propagate to changes in the delegates?

      For example, let's say I have a list with model like below, and I want the ct property to increment when I click on a button, and have that reflected in the button label:

      ListView {
          id: mylist
          model: [ {n:"a", ct:0}, {n:"b", ct:0} ]
          delegate: Button {
              text: modelData.n + modelData.ct
              onClicked: {
                  modelData.ct++;
                  console.log(JSON.stringify(mylist.model))
              }
          }
      }
      

      The code above constantly outputs the original model (where ct is 0) on each click. Same if I attempt to force a change like: mylist.model[index].ct += 1.

      How can I change the model (without using a ListModel, which does not support nested data structures I am currently using)?

      O Offline
      O Offline
      ODБOï
      wrote on 29 Jun 2018, 23:35 last edited by ODБOï
      #2

      @Phrogz hi,

      this way the ct value is incremented but model is not updating (refreshing)

      
          ListModel {
              id: _model
      
              ListElement {
                  n: "a"
                  ct: 0
              }
              ListElement {
                  n: "b"
                  ct: 0
              }
      
          }
      
          ListView {
              id: mylist
              anchors.fill: parent
              model: _model
              delegate: Button {
                  height: 20
                  width: 50
                  text: _model.get(0).n + _model.get(0).ct
                  onClicked: {
                      console.log(_model.get(0).ct += 1)
                  }
              }
          }
      

      i think this is bad but :

        onClicked: {
                      var _ct = _model.get(0).ct
                      _model.clear()
                      _model.append({"n":"a", "ct": _ct+1})
      
                  }
      

      This can help more efficiently https://doc-snapshots.qt.io/qt5-5.9/qml-qtqml-models-listmodel.html#using-threaded-list-models-with-workerscript

      1 Reply Last reply
      0

      1/2

      29 Jun 2018, 22:37

      • Login

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