3 Attachment(s)
Column and rows are showing empty in tableView
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 emptyAttachment 10340
I do not guess what is the wrong with my code.I am using the following version of Qt Attachment 10341
My database table is Attachment 10342
I am afraid what to do.
Help !!!
Thanks
Re: Column and rows are showing empty in tableView
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.
Re: Column and rows are showing empty in tableView
Thanks @Chris i got your point and i got the job done