Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Dynamic Listmodel not populating properly

Dynamic Listmodel not populating properly

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 259 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.
  • C Offline
    C Offline
    chilarai
    wrote on last edited by
    #1

    I am trying to populate a Listmodel dynamically using QML. But the problem is, in my ListView, I can see just the first element from the Listmodel.

    The signal received just notifies some process has completed and nothing more. So I Did not post the C++ code

    Please point me what am I doing wrong. Here is the rest of my code

    import QtQuick 2.12
    import QtQuick.Controls 2.4
    import QtQuick.Layouts 1.3
    
    Page {
    
        id : somepageid
    
        ListModel{
            id: someListModel
        }
    
        // Signal received here
        Connections{
            target: testModel
            onObtained:{
    
                var testModel = [["users", "name"], ["users", "surname"]]
    
                testModel.forEach(function (element) {
                    someListModel.append({"tableName" : element[0], "colName" : element[1]});
                });
    
                someListView.model =  someListModel
                someListView.height = someListModel.length * 40
            }
        }
    
    
        // List View
        // Want to display the data here
    
        ListView{
            id: someListView
            anchors.top: parent.top
            width: 150
            delegate:
                MenuItem {
                    text: colName
                }
        }
    
    }
    
    ODБOïO 1 Reply Last reply
    0
    • C chilarai

      I am trying to populate a Listmodel dynamically using QML. But the problem is, in my ListView, I can see just the first element from the Listmodel.

      The signal received just notifies some process has completed and nothing more. So I Did not post the C++ code

      Please point me what am I doing wrong. Here is the rest of my code

      import QtQuick 2.12
      import QtQuick.Controls 2.4
      import QtQuick.Layouts 1.3
      
      Page {
      
          id : somepageid
      
          ListModel{
              id: someListModel
          }
      
          // Signal received here
          Connections{
              target: testModel
              onObtained:{
      
                  var testModel = [["users", "name"], ["users", "surname"]]
      
                  testModel.forEach(function (element) {
                      someListModel.append({"tableName" : element[0], "colName" : element[1]});
                  });
      
                  someListView.model =  someListModel
                  someListView.height = someListModel.length * 40
              }
          }
      
      
          // List View
          // Want to display the data here
      
          ListView{
              id: someListView
              anchors.top: parent.top
              width: 150
              delegate:
                  MenuItem {
                      text: colName
                  }
          }
      
      }
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      hi
      @chilarai said in Dynamic Listmodel not populating properly:

      I can see just the first element from the Listmodel

      [edit] you set it in the js function is that maybe because you don't set the height property for your listview ?.
      but

            someListModel.length *40 // this returns NaN     
      

      try

      someListView.height = someListModel.count * 40
      
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          Timer{
              interval: 1500
              running: true
              repeat: true
              onTriggered: onObtained()
          }
      
          ListModel{
              id: someListModel
          }
      
          function onObtained(){
           
              var testModel = [["users", "name"], ["users", "surname"]]
      
              testModel.forEach(function (element) {
                  someListModel.append({"tableName" : element[0], "colName" : element[1]});
              });
          }
      
          ListView{
              id: someListView
              anchors.top: parent.top
              width: 150
              height: model.count * 40
              model : someListModel
              delegate: Text {
                  height: 40
                  text: tableName + " " +  colName
              }
          }
      }
      
      1 Reply Last reply
      1
      • C Offline
        C Offline
        chilarai
        wrote on last edited by
        #3

        Thank you so much. That solved it. It was indeed a silly mistake

        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