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. [Solved] The "best" way to programmatically refresh a QSqlQueryModel when the content of the query changes

[Solved] The "best" way to programmatically refresh a QSqlQueryModel when the content of the query changes

Scheduled Pinned Locked Moved General and Desktop
14 Posts 9 Posters 22.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.
  • D Offline
    D Offline
    d.oberkofler
    wrote on last edited by
    #1

    I was wondering what the appropriate way to refresh a QTableView using a QSqlQueryModel would be when the result of the query would change triggered by some external event outside the Qt app?

    I'm aware that:

    • QSqlQueryModel ::setQuery can be called but why should I parse a new query just to refresh it?
    • QTableView::setModel can be used but why should I set a new model only to refresh it?
    • QSqlQueryModel ::reset can be used but this is a protected method and would completely reset the model including information about the header line etc.

    The model/view architecture of Qt seems to have a very complex and well documented architecture but I just cannot seem to find the proper way to do this.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      I think you'll have to stick to the setQuery method. At first, you say that the query would change because of some external event. Then why is it weird that you have to set it again? It is not refreshing if the query itself has changed, is it?
      How much that costs, would depend on your db engine I think. It may - I am not sure here - also depend on how you set your query. If you use setQuery (QSqlQuery) and bind the arguments that will change, your database may be able to optimize and not parse the SQL again. If not, using a view in your database could help too.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca
        wrote on last edited by
        #3

        In my projects with mysql I use setQuery to update the data.
        With slow query, the first setQuery is slow but the next are very faster because mysql keep in cache the table used in query.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          d.oberkofler
          wrote on last edited by
          #4

          I would also guess that the performance impact very much depends on how the Qt database driver is implemented and the internals of the database itself.
          I also agree that setQuery typically should not have a huge performance impact but why should this not be encapsulated in the Qt model itself. In my impression a simple public refresh method just seems to be missing.
          To give a an example I use the situation I came across when issuing this message.
          I use a rather complex SQL query to an Oracle Database to retrieve data that will be used in a graph.
          The graph will be updated every second using a timer with the latest data and update the graph.
          The columns and the query are constant and I would only like to "refresh" the data will the latest values.

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            ZapB
            wrote on last edited by
            #5

            Another way is to prepare a database view of your complex query and then use a QSqlTableModel on the view. This class does expose an exec() method to re-run the query.

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #6

              Oops sorry, the method is actually select() not exec(). /me should check the docs.

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xj.liu.yang
                wrote on last edited by
                #7

                QSqlTableModel::select() use setQuery( QString, QSqlDatabase ) method refresh contents. I guess you could do it the same way.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Galbarad
                  wrote on last edited by
                  #8

                  Hi all
                  I have similar question I am use QTableView + QSqlQuery when I set query at first time I allow fetch only first 10 rows from query, after that according user decides I can fetch next few rows, but my QTableView is not refreshed after fetch. I can't use setQuery because I do not what run sql yet one time to get result that I already have.
                  I try to use something like this
                  @emit dataChanged(createIndex(OldRowCnt, 1), createIndex(RowCount, HeaderCount));@
                  but this has no result

                  thank you for future help )

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    Galbarad
                    wrote on last edited by
                    #9

                    beginInsertRows solve my problem, I am call it after fetch

                    @bool cOciQModel::fetch(int Cnt, bool SilentMode) {
                    if (RowCount && !query->seek(RowCount - 1)) {
                    LastError = QString("Can't goto [%1] position").arg(QString::number(RowCount - 1));
                    return false;
                    }
                    int OldRowCnt = RowCount;
                    int Count = Cnt + 1;
                    while (--Count && !AllDataFetched) {
                    AllDataFetched = !query->next();
                    if (!AllDataFetched) {
                    ++RowCount;
                    }
                    }

                    if (!SilentMode) {
                    beginInsertRows(QModelIndex(), OldRowCnt, RowCount);
                    endInsertRows();
                    }

                    return true;
                    

                    }@

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      iamantony
                      wrote on last edited by
                      #10

                      How I update QSqlQueryModel:

                      QString queryStr = model->query().executedQuery();
                      model->clear();
                      model->query().clear();
                      model->setQuery(queryStr);
                      
                      ? 1 Reply Last reply
                      0
                      • I iamantony

                        How I update QSqlQueryModel:

                        QString queryStr = model->query().executedQuery();
                        model->clear();
                        model->query().clear();
                        model->setQuery(queryStr);
                        
                        ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        @iamantony how to do it with Python?

                        jsulmJ 1 Reply Last reply
                        0
                        • ? A Former User

                          @iamantony how to do it with Python?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Gelo Replace -> with .

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          ? 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Gelo Replace -> with .

                            ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #13

                            @jsulm

                            QString queryStr = model->query().executedQuery();
                            model->clear();
                            model->query().clear();
                            model->setQuery(queryStr);

                            is that c++? for what is QString?

                            jsulmJ 1 Reply Last reply
                            0
                            • ? A Former User

                              @jsulm

                              QString queryStr = model->query().executedQuery();
                              model->clear();
                              model->query().clear();
                              model->setQuery(queryStr);

                              is that c++? for what is QString?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Gelo Yes, it is C++.
                              QString is a Qt data type for strings - no need to write it in Python.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0

                              • Login

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