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?

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