Unable to retrieve information from records within a QSqlQueryModel

  • 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):


    I tried to retrieve the information using the record function:


    This creates an error:

    'class QAbstractItemModel' has no member named 'record'

    I then tried to change to:


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


    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?


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

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

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

    delete ui;

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

  • Try this.

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

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

  • 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();@

  • Lifetime Qt Champion


    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.

  • Yes, qobject_cast make sense here. SGaist is right.

  • Thanks, SGaist!

    I've changed:

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


    @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.

  • Lifetime Qt Champion

    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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.