[Qt-interest] MVC Questions and Syncing QTreeView and QTableView

Stephen Kelly steveire at gmail.com
Tue Jan 11 19:22:25 CET 2011


Sean Harmer wrote:

> Hi,
> 
> On Tuesday 11 January 2011 16:19:48 lbutler3 at comcast.net wrote:
>> I've got some questions about how to keep multiple views of the same data
>> in sync.
>> 
>> I'm building an application to organize 3D models and manipulate
>> meta-data. Think about grouping objects together into collections (and
>> collections of collections) as well as setting attributes (like color) of
>> the leaf items.
>> 
>> The heirarchy is displayed and edited using a QTreeView which has a data
>> model class derived from QAbstractItemModel to give it access to data to
>> display. I need to display a table of "parts" (the leaves of the
>> heirarcy) and their associated properties (eg. color) for users to edit.
>> I can create a table model derived from QAbstractItemModel.

Lets use a filesystem as an analogy. Which of these do you want:

1) You have a tree of only the folders in the filesystem. When you click a 
folder you see the files in that folder only.
2) You have a tree of the entire filesystem. You also have a table of the 
same stuff, but flat, showing all of the files in the whole filesystem but 
none of the folders. This seems to be what you want, reading Seans reply, 
but I want to be sure.

>> 
>> The question is how to keep these two data models in sync.

If they model the same data, you might need one model instead of two. I 
think many of the rest of your issues fall away or are manageable if you do 
that. That's the master tree model that Sean describes.

>> They both have
>> selection models that need to be kept in sync. Contrary to the tutorials
>> and examples I've seen, neither datamodel seems appropriate to "own" the
>> underlying data. They just provide an interface for the Q*Views. For
>> example, the rendering pipeline needs access to the application-native
>> representation of the data for drawing the 3D display. So how do we map
>> QModelIndex items from one view to another? How do we keep selection
>> models in sync? And most crucially: Where does this processing take
>> place? I accept that the classes derived from QAbstractItemModel are a
>> part of the "data model" (M from Model-View-Controller). Does the
>> controller take the responsibility of reaching down into both models to
>> manipulate their selections? I guess I can see this when doing operations
>> like cut-paste, but for selection I was hoping there might be a cleaner
>> way.
>> 
>> Your counsel would be greatly appreciated.
> 
> The first thing I would try is to treat your tree-model as the "master"
> model. I would then create a proxymodel on which you set your tree model
> as the source model.
> 
> This new proxy model should simply flatten your tree model down into a
> table.
> 
> Then carry on connecting your tree model to a tree view but now also
> connect your new proxy model to a QTableView. Both views will be updated
> automatically as you change the data in the tree model since the proxy
> will also be notified when data in the source model has changed and in
> turn the proxy model will notify the table view.
> 
> So the only potentially tricky part is writing such a proxy model. It is
> worth searching the KDE source as there may well be such a model already
> available. Ah, take a look at KReparentingProxyModel:
> 
> http://api.kde.org/4.x-api/kdepim-
> apidocs/akonadi_next/html/classKReparentingProxyModel.html
> 
> I think you simply need to inherit from this class and override the
> isDescendentOf() function to always return false. This should flatten any
> tree model into a simple list. It should give you some ideas anyway.
> 
> HTH,
> 
> Sean





More information about the Qt-interest-old mailing list