PDA

View Full Version : Column and rows are showing empty in tableView



foxrider
7th May 2014, 16:19
Hello !!!

I am trying to load my data base table to tableView.I have written the following piece of code.


void UserLog::on_pushButton_7_clicked()
{
login conn;
QSqlQueryModel *modal =new QSqlQueryModel;
conn.connOpen();
QSqlQuery* qry = new QSqlQuery(conn.db);
qry->prepare("select * from `posdb`.items");
if(qry->exec())
{
modal->setQuery(*qry);
ui->tableView->setModel(modal);
conn.connClose();
}
else
{
QMessageBox::critical(this,tr("error::"),qry->lastError().text());
}

qDebug() <<(modal->rowCount());
}

It is successfully loading the table.But the problem is that the table fields are empty10340
I do not guess what is the wrong with my code.I am using the following version of Qt 10341
My database table is 10342

I am afraid what to do.
Help !!!
Thanks

ChrisW67
7th May 2014, 21:48
You immediately close the database connection that the model is using to access your database. After this the behaviour of the model is undefined. If you want a one-off load of the table data into a widget then you should use QTableWidget and iterate over a QSqlQuery to populate it before closing the connection.

It would be unusual to open/close the database connection in a button handler slot.

foxrider
8th May 2014, 06:57
Thanks @Chris i got your point and i got the job done