Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Update multiple models based on an update of data



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

    I've been reading this thread and I was wondering exactly the same thing. The last post is approximately my question (from @SGaist):

    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 ?

    Actually I'm not expecting that this happens magically, but I was wondering what it would take to have raw data updated somewhere that triggers multiple models updates and thus multiple views updates. How would the mapping between the data and the multiple models work ?

    For example:

    • I have a list of multiple Measurements which contain attributes like size, color and weight
    • One model is used as an interface with a QListView that displays only the size of all the Measurements
    • One model is used as an interface with a QTableView that displays the size and the color of all the Measurements
    • etc.

    Then I trigger an update on my raw data. How to prepend this change to all my associated models ?



  • @Leogout
    Trying to put this all together for you....

    Forget about the views. They will update automatically if & when your models get updated.

    Then I trigger an update on my raw data.

    What do you mean by this/how does it happen? In the other post the question was about an external change being made in the database side, and how that could be detected. Here you talk about you doing some kind of update? If you are updating data in the database within your program, you could re-read the new data into your model(s). If the update happens quite outside your ken, then you are looking for notifications from the database to your program that changes have been made, and I think we have previously looked into this and the database drivers used in Qt do not support this, though you should specify what your database is.



  • Thanks for your answer. I think I mislead you by mentionning the previous post. I don't have a database, I have my own data source, which is a class containing some data. If I take my previous example back, lets say I have a MeasurementsData class which contais a list of Measurements.
    I would like to base my models on this data source and prepend the changes I make to this source to the other models, either by having one of my model update it directly or by something else, like an action triggered by a button pressed.



  • @Leogout
    Then I don't think your question is connected to the other post you reference.

    I'm sorry, but not I'm as good at guessing what questioners want as some other people here! So I don't really understand your problem. If you have multiple models, and somehow changes in one should cause changes in others, then just write the necessary code to do so. Either directly (code goes "update model1; update model2;"), or if you prefer by attaching a signal on update in model1 to a slot which updates model2. I don't know what relevance "triggered by a button pressed" has, it doesn't matter how the update to model1 is performed.



  • Thanks for your time, I've posted this question on Stackoverflow as well and the answer is satisfying to me. Here it is:

    I'd consider to implement a single QAbstractListModel to provide all the data (via roles), and then use simple proxy models (QIdentityProxyModel subclasses) to map that data to the columns/roles (mainly Qt::DisplayRole) as needed for respective views.

    https://stackoverflow.com/questions/63049172/update-multiple-qt-models-based-on-a-single-data-source

    This is what I was looking for, a simple and automatic way to trigger updates based on a single source of truth. I'm not comfortable with the way you would handle the problem as your solution implies that multiple models have to know and connect to each other and the data source have to know them as well and trigger manual updates.

    Thanks for your time and no worries for the guessing part, I may have not been clear on what I wanted to achieve in the first place.

    Have a nice day.



  • @Leogout
    That's fine. I had no idea from your "multiple models updates" that your models had anything in common with one another to allow proxy models to be imposed on a single model to implement this. Evidently other people have better correct guessing/interpretation than I! :)


Log in to reply