PDA

View Full Version : Model/View Confusion in QTreeView - number of rows



themolecule
5th August 2007, 21:08
I am trying to create a model/view system for plugins. Each plugin has different attributes, which should be displayed in a QTreeView.

I am confused, however, if the view's number of rows does not match the number of items in the model.

For instance, I want to display the plugins like this:


(root)
+-Transform Nodes 2 nodes
| +-Move
| | +-Inputs
| | | input1 image
| | +-Attributes 2 attributes
| | Xpos float
| | Ypos float
| +-Rotate
| +-Inputs
| | input1 image
| +-Attributes 1 attribute
| angle float
+-Color Nodes 2 nodes
+-Brightness
| +-Inputs
| | input1 image
| +-Attributes 1 attribute
| amount float
+-etc... etc...


which leads me to believe that I have 2 groups, and 2 children in each group (potentially many of each, but for this example...), and certain rows have two columns, but others only have one. The inputs and attributes are not child items, they only exist in the view and are really members of pluginItem.

The problem is, while the model has 2 groups of 2 items, the view has 19 (and more) rows. Also, another problem is that the row number of a particular item is determined by which parents are expanded/collapsed (but this may already be contained in the QTreeView code).

Does it make more sense to:

1) create a pluginGroup class which contains pluginItem children that reports many rows when displayed, or
2) create a pluginGroup class which contains pluginItem children that contains pluginDisplayGroup children that contains pluginDisplayAttribute children, or
3) create a generic pluginRow item which may be either a child or parent and has a flag in it to determine usage, or
4) create a pluginItem array in the model, which stores the group name, where the view somehow splits the rows based on unique group names, or
5) some other framework that I'm not thinking of.

Another, more minor thing, is that if I want the view to expand/collapse based on a single click... where do I insert this trigger? Do I need a delegate?

I would like to use 4 or 1 above, but I am confused about where the additonal rows are generated... 4 makes the most sense, because that's what the model really should be.

wysota
8th August 2007, 23:45
I am confused, however, if the view's number of rows does not match the number of items in the model.
According to what you describe - it does.


which leads me to believe that I have 2 groups, and 2 children in each group (potentially many of each, but for this example...), and certain rows have two columns, but others only have one. The inputs and attributes are not child items, they only exist in the view and are really members of pluginItem.
What is "pluginItem"? What does it inherit?


The problem is, while the model has 2 groups of 2 items, the view has 19 (and more) rows.
Each of the "items" has children of its own which increases the total number of items in the view.


Also, another problem is that the row number of a particular item is determined by which parents are expanded/collapsed (but this may already be contained in the QTreeView code).
What do you need the view row number for?


5) some other framework that I'm not thinking of.
Depends what you want the model to do in the first place... The model is just a data container and the view is a mean to represent some data visually, so first you should decide what you want to visualize and what do you want to store in the model.


Another, more minor thing, is that if I want the view to expand/collapse based on a single click... where do I insert this trigger? Do I need a delegate?
The delegate (at least Qt item-view delegate) doesn't handle that. Connect to the collapsed() or expanded() signals in QTreeView using a custom slot and collapse/expand all children accordingly. Just make sure not to fall into an endless loop (for example by checking if the parent is already collapsed/expanded).