PDA

View Full Version : treeview with multiple models



BreakBad
24th July 2012, 19:56
I would like to concatenate multiple models into one tree view. When an index is selected in this treeview, I would like to be able to know what 'sub'model was selected and what index of that 'sub'model was selected.



# example data
Model1
data1
child 1
child 2

Model2:
data2
child 1
child 2

Treeview:
data 1
child 1
child 2
data 2
child 1
child 2


Purpose: If I clicked on child 2 of data 2, I would like to be able to call setModel(Model2) and setRootIndex(???) on a QTableView which I have set up as an editor.


If I implement a proxy view, what modifications do I need to make? See below:




QSortFilterProxy or QAbstractProxyModel?

data(): how do I know which model was clicked?
index(): how do I know which model to use?
parent(): ?
mapToSource(): ?
mapFromSource(): ?
rowCount(): sum of rowCount from all models
columnCount(): 1


I'm thinking the key is in mapToSource/mapFromSource and having to count what row your on. Eg: If Model 1 has 10 rows and you are on row 12, and Model 2 as 10 rows, then you are requesting row 2 from model 2. Is this how it's done or is there a more elegant approach?

TYVM,

BB

amleto
25th July 2012, 00:40
an QAbstractProxyModel can still only have one model!

Probably your best bet is to make another model that is a composition of your individual models.

Santosh Reddy
25th July 2012, 07:31
If you cannot change the mode 1 and model 2, then your approach for proxy model sounds ok. The better solution would be to have all the indexes (all 20 rows) in a single model.

BreakBad
25th July 2012, 13:06
Thanks for the replies!

The only reason I cannot have the data in a single model is because those models are being used elsewhere and I require the data in all my views to be sync'd.

I did finally find an example in the following thread which I will be exploring: http://www.qtcentre.org/threads/45342-QTreeView-auto-resize?highlight=multimodel