QTableView displays correct rows and columns but no data in cells.



  • Hello all,

    I'm having a problem displaying data in a QTableView using the QSqlQueryModel class read-only model. The tableview shows the correct number of rows and columns but contains no data from the query. (Well the headers would be some of that data.)
    I can connect to the db and am successfully collecting information from the db. I know this because I perform this section of code to inspect the data I have collected:

    int rows = model->rowCount();
    int columns = model->columnCount();
    qDebug() << "Number of rows" << rows;
    qDebug() << "Number of columns" << columns;
    
    for(int i=0; i <=rows-1; i++)
    {
        for(int k=0; k <= columns-1; k++)
            {
                QString data = model->record(i).value(k).toString();
                qDebug() << "Record "<< i+1 << "," << k+1 << "of model " << data << "!";
            }
    }
    

    Once I have collected the information from the db, this code is executed to display the data in the QTableView named "tableView"

    QSqlQueryModel *model = new QSqlQueryModel;
    model->setQuery(queryvar);
    ui->tableView->setModel(model);
    

    The perplexing thing is the tableView is updated with the number of rows of data and the correct number of columns with the correct header titles. But no data in the cells.
    I was following this guys tutorial: https://www.youtube.com/watch?v=fJIdMdYJqFk
    and reading up on model view programming: http://doc.qt.io/qt-5/modelview.html
    So far nothing has clicked.

    Does anyone have some advice for me? Thanks!



  • I started a new project and started from scratch doing exactly what VoidRelms did in his video: https://www.youtube.com/watch?v=fJIdMdYJqFk

    It worked, using the exact same query that I was using in my previous project.

    So I have some figuring to do.

    One difference is that with his tutorial i selected dialog. For my original project I selected something different.

    More to come...



  • Since starting with QT I have been using the same project to learn and develop in. It became a Frankenstein monster of a program. I started a new project and cleaned up my code. It is now. working. However I think I need to removed the QSqlQueryModel *model = new QSqlQueryModel; of code. Since I started a new project and removed this line of code everything is working as it should.


Log in to reply
 

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