Need help in QTableView



  • hi

    I am trying to display data from mysql database table in QTableView. when i run my code it displays this error
    @
    Starting /home/zafar/c++/QSqlTableModel-build-desktop/QSqlTableModel...
    The program has unexpectedly finished.
    /home/zafar/c++/QSqlTableModel-build-desktop/QSqlTableModel exited with code 0
    @

    here is my code
    @
    #include <QtGui/QApplication>
    #include <QtSql>
    #include <QTableView>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    //MainWindow w;

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setDatabaseName("testdb");
    db.setHostName("localhost");
    db.setUserName("root");
    db.setPassword("xxxxxx");
    
    bool ok = db.open();
    
    if(ok) {
        QSqlTableModel *model;
        model->setTable("testTable");
        model->select();
    
        for(int i = 1; i < model->rowCount(); i++) {
            QString fname = model->record(i).value("fname").toString();
            QString lname = model->record(i).value("lname").toString();
            //qDebug() << fname << " " << lname << "\n";
    
            model->setHeaderData(0, Qt::Horizontal, "First Name");
            model->setHeaderData(1, Qt::Horizontal, "Last Name");
    
            QTableView *view = new QTableView;
            view->setModel(model);
            view->show();
        }
    }
    else {
        qDebug() << "Database could not be opened";
    }
    
    //view->show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    how can i solve this problem? Please help



  • You are not creating a QSqlTableModel, only a pointer to it. You need to have

    @QSqlTableModel* model = new QSqlTableModel;@



  • Yes, and you don't need to do the setHeaderData(..) so many times, only once, before or after the for{..} stuff.



  • I don't think you need the loop at all (at least based on the snip above).



  • The remarks above are true, but I think you'd learn more if you try to learn how to debug your code. If you run the code through a debugger, you would have found that you have a segmentation fault at line 21, indicating the error ZapB points out. QtCreator comes with a debug mode; please learn to use it. It will give you a the stack of function calls that caused the crash. Usually, the culprit is the line closest to the top of your stack that is located in your own code and not in the Qt libs.

    Why are you creating a new table view for every row of your model?



  • thanks this problem is solved. now it is working but when i close the window it displays this
    @
    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    @

    how to remedy this?



  • Should you close the db resources before you exit? you can chk further in QSqlDatabase doc


Log in to reply
 

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