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();
}
@how can i solve this problem? Please help
-
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?