[Qt-interest] Question about Models, Roles and data

Arnold Krille arnold at arnoldarts.de
Tue Aug 25 19:23:45 CEST 2009


Hi,

On Tuesday 25 August 2009 17:59:20 Andre Somers wrote:
> Murphy, Sean M. wrote:
> > I'm slowly working my way into writing models for an existing data file
> > structure that we use for a bunch of internal applications and have a
> > couple of general questions.
> > First the file structure:  Our data file (or document) is built up of
> > two types of classes, foo and bar, where bar classes are children of foo
> > classes.  The file has 0-N foo objects, each foo object has 0-N bar
> > objects as children.
> > Since the file has this hierarchal structure, it makes sense to look at
> > the file in a tree view, so I've built a custom model from
> > QAbstractItemModel that works on the *file object* and shows the
> > relationships of all the foo's and bar's; it seems to be working well.
> > I'm realizing however that it may be handy to have all sort of different
> > ways to look into these different classes, like a list view that just
> > shows all the foo's, and a separate list view that just shows the bar's
> > for a given foo.  Also a table view that shows all the data fields of
> > all the bar's would be nice too.
> > So my question is, how many models do I create to get all the different
> > slices into my data?
> One :-)
> If you need a list view of the foo classes, just set your model on a
> list view and set the rootIndex to QModelIndex(). If you need a list
> view of all the bar classes belonging to a specific foo object, just set
> that object's model index as the root index for your list view and presto.

Yep.

> For the table that shows all the data fields of all the bars, you may
> need to build a new model (so, the answer changes to two, sorry!), as
> you would be changing parent/child relationships here.

No, a table is actually a tree with only first-level entries:-)
In other words: A tree-model can have as many columns as needed, the QTreeView 
or any simpler QTableView (showing only foos or bars) will show the columns.

> If you want to
> cheat, you might be able to hide your foo classes from the view with a
> custom delegate, but that may not be the best approach...

Don't do that. If you want to hide the foos, use a proxymodel. A delegate is 
for the presentation (and user-interaction), not for data-modification. Use a 
delegate if some fields have custom types that can't be represented by Qts 
default delegate. Or if you want to change the number of significant numbers in 
the QDoubleSpinBox. Although that last example might be possible to achieve 
without a custom delegate...

> > Is the general rule of thumb that I need one model
> > per data class per view type?  Or can I play around with custom roles to
> > reduce the number of model classes I need?
> You should be able to get almost everything you want from a single model.

And stay away from custom roles unless you really have to use them.

The main advantage of model-view is that you can plug (almost) any view on any 
model and it makes sense, preferable without using custom delegates. If you 
need custom roles, you loose all that...

> > I realize that I'm being general enough that I can't get specific
> > answers, but I'm just looking for general comments of how you go about
> > maintaining all the models for your data.

Yeah, closed source and industry espionage :-P

Have fun,

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090825/31dad10e/attachment.bin 


More information about the Qt-interest-old mailing list