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. Model/View: how to make external changes to models trigger updates in all views?
Forum Updated to NodeBB v4.3 + New Features

Model/View: how to make external changes to models trigger updates in all views?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 4.2k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rmam
    wrote on last edited by
    #1

    I have a domain model which is referred by multiple Qt-based models (mainly inherited from QAbstractListModel). I also have multiple views associated with each of those models to provide different representations of the domain model. The domain model is updated by external calls, which aren't associated with the Qt's Model/View framework, and I'm looking for a way to get updates to the data model to trigger updates in all views. Unfortunately there isn't much information on how to pull this off. I was expecting that classes such as QAbstractListModel offered slots or member functions that signaled that element X needed to be updated, but so far the closest I could find is the QAbstractItemModel::dataChanged() signal, which doesn't seem to be a solution.

    So, does anyone know of a way to get models in Qt's Model/View framework to signal all views that data has changed and requires updating?

    JonBJ VRoninV 2 Replies Last reply
    0
    • R rmam

      I have a domain model which is referred by multiple Qt-based models (mainly inherited from QAbstractListModel). I also have multiple views associated with each of those models to provide different representations of the domain model. The domain model is updated by external calls, which aren't associated with the Qt's Model/View framework, and I'm looking for a way to get updates to the data model to trigger updates in all views. Unfortunately there isn't much information on how to pull this off. I was expecting that classes such as QAbstractListModel offered slots or member functions that signaled that element X needed to be updated, but so far the closest I could find is the QAbstractItemModel::dataChanged() signal, which doesn't seem to be a solution.

      So, does anyone know of a way to get models in Qt's Model/View framework to signal all views that data has changed and requires updating?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @rmam
      What Qt View class(es) are you using?

      See http://doc.qt.io/qt-5/qabstractitemview.html#dataChanged ?

      1 Reply Last reply
      1
      • R rmam

        I have a domain model which is referred by multiple Qt-based models (mainly inherited from QAbstractListModel). I also have multiple views associated with each of those models to provide different representations of the domain model. The domain model is updated by external calls, which aren't associated with the Qt's Model/View framework, and I'm looking for a way to get updates to the data model to trigger updates in all views. Unfortunately there isn't much information on how to pull this off. I was expecting that classes such as QAbstractListModel offered slots or member functions that signaled that element X needed to be updated, but so far the closest I could find is the QAbstractItemModel::dataChanged() signal, which doesn't seem to be a solution.

        So, does anyone know of a way to get models in Qt's Model/View framework to signal all views that data has changed and requires updating?

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

        You answered your own question

        @rmam said in Model/View: how to make external changes to models trigger updates in all views?:

        does anyone know of a way [...] to signal [...] that data has changed [...] ?
        the QAbstractItemModel::dataChanged() signal

        Why do you say it's not the solution. That is exactly what it does

        "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

        R 1 Reply Last reply
        1
        • R Offline
          R Offline
          rmam
          wrote on last edited by
          #4

          @JonB getting a data source (i.e., domain model) to directly access views and circumvent the model inverts the dependency between views and models and defeats the whole point of a model/view architecture.

          JonBJ 1 Reply Last reply
          0
          • VRoninV VRonin

            You answered your own question

            @rmam said in Model/View: how to make external changes to models trigger updates in all views?:

            does anyone know of a way [...] to signal [...] that data has changed [...] ?
            the QAbstractItemModel::dataChanged() signal

            Why do you say it's not the solution. That is exactly what it does

            R Offline
            R Offline
            rmam
            wrote on last edited by
            #5

            @VRonin, QAbstractItemModel::dataChanged() requires that the changes are expressed as a 2D range (top left, bottom right), which doesn't appear the most appropriate solution when referring to elements in a list.

            In that case, how should the dataChanged() signal be emitted to trigger an update on a single list element? Should the top left and bottom right index be the same value?

            VRoninV 1 Reply Last reply
            0
            • R rmam

              @VRonin, QAbstractItemModel::dataChanged() requires that the changes are expressed as a 2D range (top left, bottom right), which doesn't appear the most appropriate solution when referring to elements in a list.

              In that case, how should the dataChanged() signal be emitted to trigger an update on a single list element? Should the top left and bottom right index be the same value?

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

              @rmam said in Model/View: how to make external changes to models trigger updates in all views?:

              Should the top left and bottom right index be the same value?

              Bingo! That is also how Qt native models do it, for example QStandardItemModel: https://code.woboq.org/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel.cpp.html#574

              "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
              2
              • R rmam

                @JonB getting a data source (i.e., domain model) to directly access views and circumvent the model inverts the dependency between views and models and defeats the whole point of a model/view architecture.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @rmam said in Model/View: how to make external changes to models trigger updates in all views?:

                @JonB getting a data source (i.e., domain model) to directly access views and circumvent the model inverts the dependency between views and models and defeats the whole point of a model/view architecture.

                Yes, I know, of course I never intended that. http://doc.qt.io/qt-5/qabstractitemview.html#dataChanged states:

                This slot is called when items with the given roles are changed in the model.

                So (for my own understanding) how is that making the model access the views, it's the views reacting to changes in the model?

                1 Reply Last reply
                2
                • R Offline
                  R Offline
                  rmam
                  wrote on last edited by
                  #8

                  I've been toying around with a model inherited from QSqlTableModel which is passed to a QTableView and unfortunately the QTableView is not updated when the the SQL table receives updates, even after emitting dataChanged for all rows.

                  Does anyone know if Qt offers any reliable way to manually update views?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    Might be a silly question but, are you sure you are passing the correct parameters to dataChanged ?
                    What views are connected to your QSqlTableModel ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    R 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      Hi,

                      Might be a silly question but, are you sure you are passing the correct parameters to dataChanged ?
                      What views are connected to your QSqlTableModel ?

                      R Offline
                      R Offline
                      rmam
                      wrote on last edited by rmam
                      #10

                      @SGaist said in Model/View: how to make external changes to models trigger updates in all views?:

                      Might be a silly question but, are you sure you are passing the correct parameters to dataChanged ?

                      Yes, I'm absolutely sure. In fact, I've also tested by passing the topleft and bottomright indexes to cover the whole table and still the table view doesn't update.

                      What views are connected to your QSqlTableModel ?

                      Just a single instance of a QTableView.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Can you show the code of your model ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        R 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          Can you show the code of your model ?

                          R Offline
                          R Offline
                          rmam
                          wrote on last edited by rmam
                          #12

                          @SGaist currently my model is a plain QSqlTableModel but apparently this problem affects in the very least all table models.

                          In fact, after a bit of digging I've found many references to this problem, including in Qt's forum.

                          Here's a link to a related discussion from 5 years ago, which is yet to receive a reply: How to refresh view when model data changes

                          Does Qt's model/view implementation offers any way to update a view?

                          JonBJ 1 Reply Last reply
                          0
                          • R rmam

                            @SGaist currently my model is a plain QSqlTableModel but apparently this problem affects in the very least all table models.

                            In fact, after a bit of digging I've found many references to this problem, including in Qt's forum.

                            Here's a link to a related discussion from 5 years ago, which is yet to receive a reply: How to refresh view when model data changes

                            Does Qt's model/view implementation offers any way to update a view?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @rmam
                            I have a couple of observations.

                            Firstly, https://forum.qt.io/topic/24062/solved-view-not-updating-when-data-changed does claim to be solved.

                            In the link you give, https://forum.qt.io/topic/33387/how-to-refresh-view-when-model-data-changes, the OP is inserting/deleting rows and expecting the dataChanged signal to be emitted. But that's not what happens for insert/delete rows.

                            http://doc.qt.io/qt-5/qabstractitemmodel.html#dataChanged

                            This signal is emitted whenever the data in an existing item changes.

                            See instead http://doc.qt.io/qt-5/qabstractitemmodel.html#rowsInserted, http://doc.qt.io/qt-5/qabstractitemmodel.html#rowsRemoved, etc.

                            Is it possible your case falls into this category?

                            Like the expert @SGaist , I am expecting these view updates to work correctly. From e.g. http://doc.qt.io/qt-5/model-view-programming.html#view-classes

                            The use of signals and slots in the model/view architecture means that changes to the model can be propagated to all the attached views, ensuring that we can always access the same data regardless of the view being used.

                            which is exactly what you are saying you expect.

                            1 Reply Last reply
                            2
                            • R Offline
                              R Offline
                              rmam
                              wrote on last edited by
                              #14

                              @JonB said in Model/View: how to make external changes to models trigger updates in all views?:

                              Is it possible your case falls into this category?

                              Unfortunately it doesn't appear to fall in the same category, because in my case the database is being updated externally (albeit through calls to QSqlQuery::query). To be more precise, a QSqlTableModel is passed to a QTableView, but the SQL database is updated through a repository that runs a bunch of SQL queries.

                              I can get the repository to fire signals when any record in the database is updated, but if Qt's model/view implementation doesn't offer any way to refresh views, either by triggering refreshes through models or the view components themselves, then the views don't get updated.

                              JonBJ 1 Reply Last reply
                              0
                              • R rmam

                                @JonB said in Model/View: how to make external changes to models trigger updates in all views?:

                                Is it possible your case falls into this category?

                                Unfortunately it doesn't appear to fall in the same category, because in my case the database is being updated externally (albeit through calls to QSqlQuery::query). To be more precise, a QSqlTableModel is passed to a QTableView, but the SQL database is updated through a repository that runs a bunch of SQL queries.

                                I can get the repository to fire signals when any record in the database is updated, but if Qt's model/view implementation doesn't offer any way to refresh views, either by triggering refreshes through models or the view components themselves, then the views don't get updated.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #15

                                @rmam
                                But the rest of us are saying Qt does offer such refreshing of views in response to model change signals.

                                The model must emit the necessary signals.

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Are you expecting QSqlTableModel to automatically get notified by the database server that it has been changed by some external process and thus trigger a refresh ?

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  2

                                  • Login

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