Trying to get records from database with c++ and pass it to listview model
-
I'm lost with all datatypes and what I'm trying to do is to get object or json to pass to qml list view model.
Lets start, I have the resuts from SELECT query and Im looping:
QJsonObject data; while (query.next()) { data.insert("firstname", query.value("FIRSTNAME").toString()); data.insert("name", query.value("NAME").toString()); }
I have like 10 results from database, but if I print this with qdebug() I get only last result.
So it doesn't append new result, but it overwrites it.Even if I make this work to append the data, then I hope in qml it will be easy to place it in listview.
Maybe QJsonObject is not way to go, any suggestion would be nice. -
What tried is next:
QJsonArray data; QJsonObject myObject; while (query.next()) { myObject.insert("firstname", query.value("FIRSTNAME").toString()); myObject.insert("name", query.value("NAME").toString()); data.push_back(myObject);
then in qml I'm trying to get this object:
Rectangle { id: rectangle color: "#2c313c" anchors.fill: parent Component.onCompleted: { cli = JSON.stringify(Clients.clients()) for (var i = 0; i < cli.length; i++) { console.log(cli[i] + "<br>") } } ListView { anchors.fill: parent anchors.margins: 20 clip: true model: Clients.clients() delegate: spaceManDelegate section.property: "nation" section.delegate: sectionDelegate } Component { id: spaceManDelegate Item { width: ListView.view.width height: 20 Text { anchors.left: parent.left anchors.verticalCenter: parent.verticalCenter anchors.leftMargin: 8 font.pixelSize: 12 text: name color: "#b9b5b5" } } } Component { id: sectionDelegate Text { width: ListView.view.width height: 20 text: section } }
I get next error:
qrc:/qml/views/clients/read.qml: ReferenceError: name is not defined -
On the QML site when I do next:
Component.onCompleted: { var cli = Clients.clients() console.log(cli) for (var i = 0; i < cli.length; i++) { console.log(cli[i].name + " : " + cli[i].firstname) } }
I get the valid results....but format does spaceManDelegate want?
-
Text { anchors.left: parent.left anchors.verticalCenter: parent.verticalCenter anchors.leftMargin: 8 font.pixelSize: 12 text: modelData.name color: "#b9b5b5" Component.onCompleted: { console.log(JSON.stringify(modelData.name)) } }
so modelData.name does the trick....maybe there are better ways, if someone hve any suggestion, please share