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. Using QSqlQueryModel class in QML

Using QSqlQueryModel class in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.2k 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.
  • behruz montazeriB Offline
    behruz montazeriB Offline
    behruz montazeri
    wrote on last edited by
    #1

    I want to populate QSqlQueryModel in QML TableView

    #include "model.h"
    
    Model::Model(QObject *parent) :
        QSqlQueryModel(parent)
    {
    
    }
    
    
    QVariant Model::data(const QModelIndex & index, int role) const {
    
    
        int columnId = role - Qt::UserRole - 1;
    
        QModelIndex modelIndex = this->index(index.row(), columnId);
    
    
        return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
    }
    
    QHash<int, QByteArray> Model::roleNames() const {
    
        QHash<int, QByteArray> roles;
        roles[IDRole] = "ID";
        roles[TitleRole] = "Title";
        roles[PoemRole] = "Poem";
        roles[GrpRole] = "Grp";
        return roles;
    }
    
    
    main.cpp
    
    
    
        DataBase database;
        Model *model = new Model();
        model->setQuery("SELECT " TABLE_ID ", " TABLE_TITLE ", " TABLE_POEM ", " TABLE_GRP
                       " FROM " TABLE);
        engine.rootContext()->setContextProperty("myModel", model);
    
    
    
    TableViewColumn {
                role: "IDRole"    
                title: "Id"
            }
    
            TableViewColumn {
                role: "TitleRole"    
                title: "Title"
            }
    DiracsbracketD 1 Reply Last reply
    0
    • behruz montazeriB behruz montazeri

      I want to populate QSqlQueryModel in QML TableView

      #include "model.h"
      
      Model::Model(QObject *parent) :
          QSqlQueryModel(parent)
      {
      
      }
      
      
      QVariant Model::data(const QModelIndex & index, int role) const {
      
      
          int columnId = role - Qt::UserRole - 1;
      
          QModelIndex modelIndex = this->index(index.row(), columnId);
      
      
          return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
      }
      
      QHash<int, QByteArray> Model::roleNames() const {
      
          QHash<int, QByteArray> roles;
          roles[IDRole] = "ID";
          roles[TitleRole] = "Title";
          roles[PoemRole] = "Poem";
          roles[GrpRole] = "Grp";
          return roles;
      }
      
      
      main.cpp
      
      
      
          DataBase database;
          Model *model = new Model();
          model->setQuery("SELECT " TABLE_ID ", " TABLE_TITLE ", " TABLE_POEM ", " TABLE_GRP
                         " FROM " TABLE);
          engine.rootContext()->setContextProperty("myModel", model);
      
      
      
      TableViewColumn {
                  role: "IDRole"    
                  title: "Id"
              }
      
              TableViewColumn {
                  role: "TitleRole"    
                  title: "Title"
              }
      DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by
      #2

      @behruz-montazeri
      At least, state your problem.

      1 Reply Last reply
      0
      • behruz montazeriB Offline
        behruz montazeriB Offline
        behruz montazeri
        wrote on last edited by
        #3

        Database is connected but TableView is empty. I hard search google but i couldn't find similar example.

        DiracsbracketD 1 Reply Last reply
        0
        • behruz montazeriB behruz montazeri

          Database is connected but TableView is empty. I hard search google but i couldn't find similar example.

          DiracsbracketD Offline
          DiracsbracketD Offline
          Diracsbracket
          wrote on last edited by Diracsbracket
          #4

          @behruz-montazeri

          Did you have a look at this:
          https://forum.qt.io/topic/51219/solved-qsqlquerymodel-for-qml-tableview/

          1 Reply Last reply
          0
          • behruz montazeriB Offline
            behruz montazeriB Offline
            behruz montazeri
            wrote on last edited by
            #5

            @Diracsbracket

            I saw the page but hard to read and understand . I uploaded my project : https://github.com/sudebiar/QSqlQueryModel-Class. take look at it if it's possible.

            surajit_senS 1 Reply Last reply
            0
            • behruz montazeriB behruz montazeri

              @Diracsbracket

              I saw the page but hard to read and understand . I uploaded my project : https://github.com/sudebiar/QSqlQueryModel-Class. take look at it if it's possible.

              surajit_senS Offline
              surajit_senS Offline
              surajit_sen
              wrote on last edited by
              #6

              @behruz-montazeri

              Since the role names of TableViewColumn is case-sensitive and must match a role returned by roleNames() of your model , your table view should be look like this to solve the problem

              TableView {
              anchors.fill: parent

                  TableViewColumn {
                      role: "ID"    // These roles are roles names coincide with a C ++ model
                      title: "Id"
                  }
              
                  TableViewColumn {
                      role: "Title"    // These roles are roles names coincide with a C ++ model
                      title: "Title"
                  }
              
                  TableViewColumn {
                      role: "Poem"  // These roles are roles names coincide with a C ++ model
                      title: "Poem"
                  }
              
                  TableViewColumn {
                      role: "Grp" // These roles are roles names coincide with a C ++ model
                      title: "Group"
                  }
              
                  // We set the model in the TableView
                  model: myModel
              }
              
              1 Reply Last reply
              2
              • behruz montazeriB Offline
                behruz montazeriB Offline
                behruz montazeri
                wrote on last edited by
                #7

                Now TableView has my records.Thanks.

                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