PDA

View Full Version : Which Common Model can be used for Tree View Display and List View Display ?



abk883
3rd May 2010, 10:42
Hi

I want to create a Model which stores Data and displays in both TreeView and Lsit View Format. What is the best way to go about it. Do I use some Standard model or Subclass some existing. Also I want to customize the way the data columns are to be stored. Please Reply or Guide me to some thread if already existing

Thanks a lot

Talei
3rd May 2010, 15:20
I think that depends on src data, if "layout/structure" of it is in table meaner so it's only natural to go wit TableModel. Also If you need some special/custom functionality subclassing model is a way to go.
For example, here is a QStandardItemModel with different views.

4584 4585 4586

As you can see I used ComboBox/ListView(icon mode) / TableView and everything looks nice. I stored data and setup views to display data that is needed at the time. I prepared in advance (when populating model) all the data that I need and only retrieve them when needed.
Also, when adding data to the model, You can specify the role that data can perform, so View know how to handle them, i.e. for QImage I added Qt:DecorationRole, and for QString Qt::DisplayRole so views know haw to process them.

I hope this shed some light of the subject
Regards.

abk883
3rd May 2010, 19:27
Hey Talei ..
thanks a lot for your reply

The Problem for me is I want to use a Single model for Tree View Display as Well as List View Display
So when I do this, I am unable to see the child items in the List View which I created using Standard Model. Only parent Items are dispalyed
e.g

Display in Tree View:
A
b
C
d
When I do a List View display of Same, I only see
A
C

Whereas I would like to do some filtering to display
b
d

Thanks

Talei
3rd May 2010, 20:34
Does b/d are another column in Your model? If so it seams that You listView display only colum 0, with is default, simply use:

listView->setModelColumn( 1 ); or any other column number, that you want to display. ListView displays only one column in view.

abk883
4th May 2010, 09:03
No they are not another column, they are rows which are children of A and C.

The display Got Formatted in last post, Actual Tree View is :
A
(Tab)b
C
(Tab)d

This is how I am creating the Model:

QStandardItemModel *Treemodel = new QStandardItemModel(0,1);

QStandardItem* item1 = new QStandardItem("A");
QStandardItem* item2 = new QStandardItem("C");

QStandardItem* item3 = new QStandardItem("b");
QStandardItem* item4 = new QStandardItem("d");

item1->appendRow(item3);
item2->appendRow(item4);

So, a TreeVIew for this Model displays all items but A ListView only displays A and C. I want all of them to be displayed on which I can apply some filter to show only Children
Thanks

Talei
4th May 2010, 16:56
AFAIK in your current implementation You can't do that, because ListView is one-dimensional list (that's why You can see item1/2, they are parents for children items), so there is, AFAIK "no way" to display 2d matrix. Workaround would be to create 1D list, but You will loose all hierarchy information that way.
Also if You want to show only children in ListView, you can traverse through all parents items (item1/2), build model and display only that in ListView.

JD2000
4th May 2010, 18:44
Your treemodel will index every element added to the tree.

For any given item in the tree you can get the other elements at the same level using the sibling function.

For more info take a look at the documentation for QModelIndex.

ChrisW67
4th May 2010, 23:06
Store your data in a basic table model with a the hierarchy you want for your tree view. If the children you want to show in your list view are all of the same parent then you might consider using a single-columned tree view and QTreeView::setRootIndex(). If not, as would seem to be the case in your example, write a QAbstractItemProxyModel derivative to filter the underlying table to produce a single-columned list for your list view.

I think the browser example in the Qt distribution/demos contains an implmentation described here http://labs.trolltech.com/blogs/2008/03/25/advanced-example-of-modelview/

abk883
6th May 2010, 11:17
Thanks a lot guys.... Tried a bit but the solution seems to be somewhat complex ..Going with a Two Model Strategy right now and will try on the suggestion again
sometime later .