PDA

View Full Version : how can I refresh QSqlQueryModel?



hashb
15th June 2009, 08:51
Hi,All

I'am using QSqlQueryModel ,but I am wondering how can I refresh the data inside it,
for example:
//

QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT name, salary FROM employee");
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));

QTableView *view = new QTableView;
view->setModel(model);
view->show();
//
if I insert a row into employee using another application,
how can i refresh the QSqlQueryModel?

Thanks.

wysota
15th June 2009, 18:23
clear() the model and set the query again. By the way, in the above situation it is better to use QSqlTableModel.

hashb
20th June 2009, 03:29
Hi,wysota

Thanks a lot for your help,
I have tried it,but it seemed that the corresponding view did not update,any suggestions?

Thanks,
hashb

hashb
20th June 2009, 04:39
Hi,wysota

I have made a mistake:
QSqlQuery q = pModel_->query();
pModel_->clear();
pModel_->setQuery(q); // <--can not work

change it to works:
QSqlQuery q = pModel_->query();
pModel_->clear();
pModel_->setQuery(q.executedQuery()); //works now!!



Thanks,
hashb