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. Unable to retrieve information from records within a QSqlQueryModel
Forum Updated to NodeBB v4.3 + New Features

Unable to retrieve information from records within a QSqlQueryModel

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 1.7k Views 1 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.
  • R Offline
    R Offline
    RudderDuck
    wrote on last edited by
    #1

    Dear all,

    This is probably a trivial problem but I cannot find the solution to it. Basically it is as follows. I have created a Qt Widgets Application. The main window contains a model-based list view and a push button. The list view is filled with records from a (MySQL) database. When I press the push button I want to get the information from the record associated with the currently selected list view item. In other words, I want to retrieve information from the model, not from the list view.

    I know that it is possible to extract information from records within a QSqlQueryModel (qt-project.org/doc/qt-5/qsqlquerymodel.html#record):

    model.record(4).value("salary").toInt();

    I tried to retrieve the information using the record function:

    qDebug()<<ui->listView->model()->record(0);

    This creates an error:

    'class QAbstractItemModel' has no member named 'record'

    I then tried to change to:

    qDebug()<<ui->listView->model();

    This creates no error and when I press the push button the debug output is:

    QSqlQueryModel(0x9386140)

    What I do not understand is why it is first recognized as a QAbstractItemModel (which does not have the record function) and then as a QSqlQueryModel (which does have the record function).

    How should I proceed to retrieve the information using the record function?

    Thanks.

    Here is an example, although the database connection is left out (this works).

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    if ( !connOpen() ) {
    qDebug()<<"Failed to open the database";
    return;
    }
    // fill list view with data from database
    QSqlQueryModel
    model = new QSqlQueryModel();
    QSqlQuery* qry=new QSqlQuery(mydb);
    qry->prepare("SELECT Type FROM CourseWorkType");
    qry->exec();
    model->setQuery(*qry);
    ui->listView->setModel((model));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    qDebug()<<ui->listView->model(); // works
    //qDebug()<<ui->listView->model()->record(0); // creates error message
    }
    @

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Try this.

      @QSqlQueryModel sqlModel = (QSqlQueryModel)ui->listView->model();
      sqlModel->record(1)@

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • T Offline
        T Offline
        turaz
        wrote on last edited by
        #3

        Hi,
        Try:
        @QSqlQueryModel sql_model = static_cast <QSqlQueryModel> (ui->listView->model());
        qDebug() << sql_model->record(0); @

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RudderDuck
          wrote on last edited by
          #4

          For the record (no pun intended), both solutions work. Thanks Dheerendra and turaz!

          It returns:

          QSqlRecord( 1 )
          " 0:" QSqlField("Type", QString, length: 150, precision: 0, required: yes, generated: yes, typeID: 253) "Preparing"

          I can now retrieve the string "Preparing" from the record as follows:

          @qDebug() << sql_model->record(0).value("Type").toString();@

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

            Hi,

            Both works but both are wrong techniques. When casting a QObject use qobject_cast. C style cast nor static_cast do any check to guarantee that the pointer you are casting can in fact be casted to the new type.

            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
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              Yes, qobject_cast make sense here. SGaist is right.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • R Offline
                R Offline
                RudderDuck
                wrote on last edited by
                #7

                Thanks, SGaist!

                I've changed:

                @QSqlQueryModel sql_model = static_cast <QSqlQueryModel> (ui->listView->model());@

                To:

                @QSqlQueryModel sql_model = qobject_cast <QSqlQueryModel> (ui->listView->model());@

                I'm assuming that it would create an error message if the pointer I'm casting cannot be casted to the new type.

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

                  To be completely safe, a check that sql_model is non zero should be added. qobject_cast will return 0 if it can't successfully cast

                  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
                  0

                  • Login

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