Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to refresh QSqlQueryModel?
Forum Updated to NodeBB v4.3 + New Features

How to refresh QSqlQueryModel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 3.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • devhobbyD Offline
    devhobbyD Offline
    devhobby
    wrote on last edited by devhobby
    #1

    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()
    { 
         _myModel->query().exec();
    }
    

    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!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • devhobbyD Offline
        devhobbyD Offline
        devhobby
        wrote on last edited by
        #3

        That seems to work!!!

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

        VRoninV JonBJ 2 Replies Last reply
        0
        • devhobbyD devhobby

          That seems to work!!!

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

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • devhobbyD devhobby

            That seems to work!!!

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

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved