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. Retrieving database data to qtablewidget
Forum Updated to NodeBB v4.3 + New Features

Retrieving database data to qtablewidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 5.7k Views 2 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.
  • L Offline
    L Offline
    Lasith
    wrote on last edited by
    #1

    I am new to qt programming! I found a video tutorial in which data is being retireved to a qtableview! But since I am new model view programming seems to be bit complex! The following method is ueds to retrieve data to a qtableview(It works)!

    void MainWindow::on_pushButton_4_clicked()
    {
    MainWindow conn;

        QSqlQueryModel * modal = new QSqlQueryModel();
        conn.connOpen();
        QSqlQuery* qry = new QSqlQuery(conn.mydb);
        qry->prepare("select user from usertable  where NAME='" +ui->textEdit->toPlainText()+ "'");
        qry->exec();
        modal->setQuery(*qry);
        ui->tableView_2->setModel(modal);
    
        conn.connClose();
    

    }
    conn is used to provide the database connection! How can I change the above method to retreive data to a qtablewidget?

    jsulmJ 1 Reply Last reply
    0
    • L Lasith

      I am new to qt programming! I found a video tutorial in which data is being retireved to a qtableview! But since I am new model view programming seems to be bit complex! The following method is ueds to retrieve data to a qtableview(It works)!

      void MainWindow::on_pushButton_4_clicked()
      {
      MainWindow conn;

          QSqlQueryModel * modal = new QSqlQueryModel();
          conn.connOpen();
          QSqlQuery* qry = new QSqlQuery(conn.mydb);
          qry->prepare("select user from usertable  where NAME='" +ui->textEdit->toPlainText()+ "'");
          qry->exec();
          modal->setQuery(*qry);
          ui->tableView_2->setModel(modal);
      
          conn.connClose();
      

      }
      conn is used to provide the database connection! How can I change the above method to retreive data to a qtablewidget?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Lasith Using QTableWidget would be even more complex as you would need to fill the table manually parsing the query result. But if you really want then see here: http://doc.qt.io/qt-5/qsqlquery.html

      QSqlQuery query("SELECT country FROM artist");
      while (query.next()) {
          QString country = query.value(0).toString();
          doSomething(country);
       }
      

      With query.value(0) you get value of first column, with query.value(1) the value from second and so on. Use http://doc.qt.io/qt-5/qtablewidget.html#setItem to set each cell.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Lasith Using QTableWidget would be even more complex as you would need to fill the table manually parsing the query result. But if you really want then see here: http://doc.qt.io/qt-5/qsqlquery.html

        QSqlQuery query("SELECT country FROM artist");
        while (query.next()) {
            QString country = query.value(0).toString();
            doSomething(country);
         }
        

        With query.value(0) you get value of first column, with query.value(1) the value from second and so on. Use http://doc.qt.io/qt-5/qtablewidget.html#setItem to set each cell.

        L Offline
        L Offline
        Lasith
        wrote on last edited by
        #3

        @jsulm Thanx mate

        1 Reply Last reply
        1
        • K Offline
          K Offline
          karlheinzreichel
          wrote on last edited by
          #4

          As in QTableWidget the setModel method is private you cannot use the model directly for setting data into a QTableWidget.

          One possible solution would be that you add every single cell of the table as a QTableWidgetItem by hand.
          In my opinion this solution would be more complex than working with models.

          BTW: the code you posted as a sample is not really worth to mention.
          e.g. every time you press your pushbutton4 a new model will be allocated and never released!

          regards
          Karl-Heinz

          1 Reply Last reply
          3
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            To add to @karlheinzreichel, there's no need to allocate QSqlQuery on the heap like you do. Just put it on the stack.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • R Offline
              R Offline
              Richard Lee
              wrote on last edited by
              #6

              You can use QSqlTableModel instead to do this.
              //create a database object
              QSqlDatabase db = QSqlDatabase::addDatabase(driver, name);
              db.setDatabaseName(database_path);
              db.open();

              //bound the database to the QSqlTableModel
              QSqlTableModel *model = new QSqlTableModel(this, db);
              model.setTable(table_name);
              model.select();

              //Now you can retrieve data from the database via the model
              QModelIndex idx = model.index(row, column);
              model.data(idx);

              For details, you can search the official reference online or in QtCreator:
              Ctrl + K,
              then type: ? QSqlTableModel.

              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