Unsolved 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.
-
@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? -
@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
-
@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?
-
I've been toying around with a model inherited from
QSqlTableModel
which is passed to aQTableView
and unfortunately theQTableView
is not updated when the the SQL table receives updates, even after emittingdataChanged
for all rows.Does anyone know if Qt offers any reliable way to manually update views?
-
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 ? -
@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
. -
Can you show the code of your model ?
-
@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?
-
@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.
-
@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, aQSqlTableModel
is passed to aQTableView
, 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.
-
@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.
-
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 ?