[Qt-interest] Question about Models, Roles and data
Andre Somers
andre at familiesomers.nl
Tue Aug 25 17:59:20 CEST 2009
Hi,
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.
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. 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...
> 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.
> 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.
>
>
I hope this helps anyway. I am using something similar in my database
structure model. Sometimes I need a list of all the tables, sometimes a
complete tree structure with tables, views and fields, sometimes only a
list of fields... All these views are based on the same model. Works
very nicely.
André
More information about the Qt-interest-old
mailing list