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. Trying to get records from database with c++ and pass it to listview model
Forum Updated to NodeBB v4.3 + New Features

Trying to get records from database with c++ and pass it to listview model

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 1 Posters 248 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.
  • D Offline
    D Offline
    daka
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      daka
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        daka
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          daka
          wrote on last edited by
          #4
          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

          1 Reply Last reply
          1

          • Login

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