How do I refresh a model so it takes new rows added after its creation?
-
I have a main model on table passwords that has a link to table location. In a different dialog I have a model based on the table location and I insert one location in that dialog. when I return to my main window, the new location is not displayed in the combobox of locations. How can I force the attached model to refresh the table location?
-
As @mpergand says for your current situation: within your program make sure you call
insertRows()
, then any attached views will be notified and refresh accordingly. Depending on your Qt model type that should already be the case (e.g. for theQSql...
models). If you claim to be doing that yet not seeing the update, make sure your two views are sharing the same model instance. If you have two separate model instances both open on the same underlying SQL database that is no use: Qt won't recognise that to propagate the signal notification from one instance to another.@jdent said in How do I refresh a model so it takes new rows added after its creation?:
@jdent The new locations could be added directly to the Database by means of a client application - how do I refresh the locations displayed?
Assuming you mean some other application, no matter what/how, updates the database you are using for your model, be aware that Qt offers no way of detecting this or acting on it. That would require (a) the native database/driver having a way of "notifying" other client applications such as yours that this has happened which most do not provide and (b) Qt having a way of receiving such a "notification" and acting on it/passing it onto you, which it does not. You would have to write your own code outside of Qt to handle this if it were possible (but probably not because of (a)).
The only way you would see such a change is (a) if you were in charge of the other application such that it could send a message to your application via a socket or shared memory/semaphore or similar or (b) you will simply see the new data the next time you (happen to) fill the model from the database. To that end if you had to know you would have to set up e.g. a
QTimer
to keep re-querying the database in case it had changed, which obviously is not very efficient. -
@jdent
Have a look at begin/end methods in QAbstractItemModel ClassThis link should be of interest:
https://stackoverflow.com/questions/76603422/update-a-qtableview-entirely-when-data-has-changed -
As @mpergand says for your current situation: within your program make sure you call
insertRows()
, then any attached views will be notified and refresh accordingly. Depending on your Qt model type that should already be the case (e.g. for theQSql...
models). If you claim to be doing that yet not seeing the update, make sure your two views are sharing the same model instance. If you have two separate model instances both open on the same underlying SQL database that is no use: Qt won't recognise that to propagate the signal notification from one instance to another.@jdent said in How do I refresh a model so it takes new rows added after its creation?:
@jdent The new locations could be added directly to the Database by means of a client application - how do I refresh the locations displayed?
Assuming you mean some other application, no matter what/how, updates the database you are using for your model, be aware that Qt offers no way of detecting this or acting on it. That would require (a) the native database/driver having a way of "notifying" other client applications such as yours that this has happened which most do not provide and (b) Qt having a way of receiving such a "notification" and acting on it/passing it onto you, which it does not. You would have to write your own code outside of Qt to handle this if it were possible (but probably not because of (a)).
The only way you would see such a change is (a) if you were in charge of the other application such that it could send a message to your application via a socket or shared memory/semaphore or similar or (b) you will simply see the new data the next time you (happen to) fill the model from the database. To that end if you had to know you would have to set up e.g. a
QTimer
to keep re-querying the database in case it had changed, which obviously is not very efficient. -