[Qt-interest] MVC Questions and Syncing QTreeView and QTableView
lbutler3 at comcast.net
lbutler3 at comcast.net
Tue Jan 11 23:17:34 CET 2011
Stephen / Sean
To use your analogy, it is more like option 2: I have one QTreeView where I display all the folders and all the files in the folders along with the creation time for each file or folder. The QTableView has only the files listed, along with their file sizes and owners. The QTableView is sorted in alphabetical order, or optionally file size order, not in filesystem tree order. It displays all of the files, not just the ones in a particular folder.
If I try to have 1 model (certainly convenient), then the model needs to know what view is asking the question in order to be able to provide the answer. For example, column2 of the QTreeView is the creation time, while column2 of the QTableView is the file size. Row 3 of the QTreeView might be a folder, but row 3 of the QTableView would be a file. Making that work with 1 model didn't seem straightforward.
Lee
----- Original Message -----
From: "Stephen Kelly" <steveire at gmail.com>
To: qt-interest at trolltech.com
Sent: Tuesday, January 11, 2011 1:22:25 PM
Subject: Re: [Qt-interest] MVC Questions and Syncing QTreeView and QTableView
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
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110111/d638f9a8/attachment.html
More information about the Qt-interest-old
mailing list