Dynamic Listmodel not populating properly
-
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 } } } -
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 } } }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 ?.
butsomeListModel.length *40 // this returns NaNtry
someListView.height = someListModel.count * 40Window { 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 } } }