Important: Please read the Qt Code of Conduct -

How to refresh QSqlQueryModel?

  • Hi.

    I have a TableView in my project that uses a QSqlQueryModel.

    The information is well displayed on the table, however, when the data in the database changes I want to "refresh" and "reload" the table... I can't understand how!

    Suppose I want to update the tableview whenever I click a button:

    void MainWindow::on_btnRefresh_clicked()

    This doesn't work.

    The tableDataChanged signal is useless in this case, because i'm not changing the data directly from the table GUI, but I just want to refresh the table itself since the database is being updated by other sources and programs.

    Thank you in advance!

  • try _myModel->setQuery(_myModel->query());

  • That seems to work!!!

    May you explain me why did setQuery() do it whereas query().exec() didn't?

  • query().exec() actually refreshes the query but the model simply doesn't know it happened as you are bypassing it, setQuery asks the model to refresh the query so it know it should look out for changes

  • @devhobby

    For @VRonin to confirm my understanding:

    QSqlQueryModel::setQuery() is a member of the model, QSqlQueryModel::query().exec() is just a method on the query. So the model knows about the former and refreshes, but does not know you're doing the latter.

Log in to reply