PDA

View Full Version : Using a TreeModel with a treeView and tableView



sheik482
25th September 2010, 06:41
I have a TreeModel that is displayed in a tree View. I also have a proxyModel that sets its source to the TreeModel and then gets displayed in a table view. The problem I am having is only the parent nodes are getting displayed in the table view.

My tree looks like the following:

parent1
| |_Child1_1
| |_Child1_2
|
parent2
| |_Child2_1
| |_Child2_2
|
parent3

My table looks like:
parent1
parent2
parent3

I would like to have it look like:

parent1
Child1_1
Child1_2
parent2
Child2_1
Child2_2
parent3

Here is a brief snipit of how I set everything up


TreeModel *model = new TreeModel(headers, file.readAll());
treeView->setModel(model);

mProxyModel = new MySortFilterProxyModel(model);
mProxyModel->setDynamicSortFilter(true);
mProxyModel->setSourceModel(model);

this->tableView->setModel(mProxyModel);
this->tableView->setSortingEnabled(true);
this->tableView->sortByColumn(0, Qt::AscendingOrder);


Does anyone know how I can go about displaying the children nodes in the table?

ecanela
25th September 2010, 07:56
you need use a proxy model for "flatness" the tree model.

create a subclass of QAbstractProxyModel, and reimplement the funcion parent() to redirect the parent of child nodes to the rootIndex() instead of the original parent, remember you need to reimplement all the virtual function of QAbstractProxyModel


http://img27.imageshack.us/img27/6472/proxytreemodel.png


also check the link http://doc.trolltech.com/4.5/model-view-model.html
and the example of a transpose proxy model http://www.qtcentre.org/wiki/index.php?title=Transpose_proxy_model


PS. sorry my poor skills using paint ;)

sheik482
27th September 2010, 04:25
I am a bit confused on how to reimplement parent() any tips?