PDA

View Full Version : Cannot see the header labels SQL



sujan.dasmahapatra
26th June 2011, 13:04
Dear Friends

I am trying to generate a table with the code below. But I cannot see the header labels in the view. Only a blank page is coming. Please give me some suggestion. Thanks sujan

QSqlQueryModel *model = new QSqlQueryModel;
model->setTable("student");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("studentID"));
model->setHeaderData(1, Qt::Horizontal, tr("Name"));
model->setHeaderData(2, Qt::Horizontal, tr("FirstName"));
model->database().transaction();
qDebug() << model->database().commit();
model->submitAll();
QTableView *view = new QTableView;
view->setModel(model);
view->show()

Lykurg
26th June 2011, 23:32
Where did you set up the database connection? And for a QSqlQueryModel you have to perform a query. If you want to set a table use QSqlTableModel.

ChrisW67
27th June 2011, 01:38
Your sample code won't compile for the reason pointed out by Lykurg. Once that is corrected the code works fine for me.

You should get into the habit of checking return values and, when they indicate errors, QSqlDatabase::lastError() or QSqlQueryModel::lastError().
QSqlTableModel::select() returns a true/false indication you are ignoring.