Results 1 to 8 of 8

Thread: Updating a View when Model Changes Externally

  1. #1

    Default Updating a View when Model Changes Externally

    Hello,

    I'm trying to learn how to use Qt's Model/View framework, and i am stuck with a "simple" problem. I've a 'composite' class called Composite that contains other classes, either Composite(s) or Leaf(s). I have subclassed a QAbstractItemModel and rendered my hierarchy of Composite items in a QTreeView. What i would like to ask, is how to update a view when i know that the model (or some part of it) has changed outside the view. If a single Composite gets changed, deleted or something else, externally, how can i inform the view ? Should i reload the model ? There is a signal called dataChanged but it needs two QModelIndex arguments which i don't have, am i correct ? Am i missing something ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Updating a View when Model Changes Externally

    You should avoid situations where a model can change "externally". You should encapsulate the data directly in your model and provide some API to modify it so that you can make sure proper signals will get emitted.

    http://blog.wysota.eu.org/index.php/...ta-redundancy/
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Updating a View when Model Changes Externally

    There is also modelReset(), if you can't know specifically what portion of the model changed.

  4. #4

    Default Re: Updating a View when Model Changes Externally

    Thanks for your answers, but let me make the problem a little more specific. Let's say we have a drawing program, and the user has created a set of points (x,y,z). He can see and manipulate the points in a QListView. However, due to 'legacy' code, these actual 'point' data structures are not in a QAbstractItemModel, but in a container of some form that we cannot alter. Now, while the list is open, he can alter the points from the list, and graphic view should update accordingly. Also, while the list is open, he can pick points from the graphic area, move them, delete them, or alter them somehow, and the list should update also. How would you deal with such a case ? I assumed that it is a good idea to gather all events (delete, edit, copy etc) in a central class (something like a controller class, or mediator) and then propagate the events to the model. So, if a point is altered from the graphic area, a specific class would receive an 'ALTER' event and the pointer to the point. Then what ? How could i ask the model to update this point only, since i have only a pointer to the point struct ? Should i hold a mapping of some sort, from pointers to QModelIndex ?

    ps: Updating the whole list could be an option if all altered points could be received in a batch, and not one after the other which is the case.
    Last edited by jstark; 16th September 2009 at 20:02. Reason: clarification

  5. #5
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Updating a View when Model Changes Externally

    try to use the signal dataChanged() in the QAbstractItemModel class for enforce a update in the views.

    if the data was modified for a external class. emit a signal to notify the change and connect whit one slot in the model. and emit the signal dataChanged. the only problem are indicate what modelIndex was modified.

    i hope you found this useful.
    Last edited by ecanela; 21st November 2009 at 06:50. Reason: reformatted to look better

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Updating a View when Model Changes Externally

    I somehow missed this post earlier...

    Quote Originally Posted by jstark View Post
    I assumed that it is a good idea to gather all events (delete, edit, copy etc) in a central class (something like a controller class, or mediator) and then propagate the events to the model.
    The model is your central/controller class. Just make everything go through the model emitting proper signals. You can extend the model with your own methods - as long as you'll be emitting signals, everything will be fine. That's what the link I provided in the previous post explains.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Updating a View when Model Changes Externally

    Hi All,

    it appears to me that this kind of problem actually occurs quite often in real-world problems. For example, in our code we have quite a large data model (thermal simulation model for a whole building including detailed geometry and appliances) which is built-up hierarchically. In our user interface, we have different views that only show certain parts of the model, sometimes in table views and sometimes in tree views.

    To fill the different table and tree views with data, we have several implementations of QAbstractItemModel and QAbstractTableModel which source the data directly from our object hierarchy, this from the model's point of view the data is external.

    Now often we have the problem that part of the data in our object hierarchy changes, for example through interaction with one model. Other models that show the same data in a different representation do not know that the external source data has changed and we have the same problem as mentioned in the thread here, that we need to tell each model implementation which part of the data has changed.

    This is difficult to do properly, so here are some options:
    1. Use brute force and tell every model to reset(). This is bad because selection and item states are lost. It works ok, when the complete object hierarchy is newly populated (e.g. after loading the project data from file), but for minor changes it is overkill.
    2. Use each model's data() function to determine which model indices have changed. This is slow and doesn't work well in many cases when type conversions are done inside the data() function.
    3. Let each model cache the data it shows so that the models can always check for differences between the model's data representation and the source data and emit the dataChanged() signal accordingly. This is probably the best way, however, it has quite a memory overhead and is sometimes tricky to implement for non-trivial models.


    Maybe you could comment on these and provide alternatives if possible?
    Andreas

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Updating a View when Model Changes Externally

    You should have one model inheriting QAbstractItemModel and all your "submodels" should just be proxies over the base model. If needed, you can add your own methods (and signals) to those classes, just don't separate the models - otherwise you will immediately run into synchronization problems. And make sure proper signals from QAbstractItemModel API are always emitted from the custom methods where appropriate.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    ecanela (23rd November 2009)

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Model / View - Design Question
    By antarctic in forum Qt Programming
    Replies: 8
    Last Post: 8th June 2009, 07:39
  3. Replies: 1
    Last Post: 14th January 2009, 10:10
  4. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  5. Replies: 6
    Last Post: 20th April 2006, 10:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.