PDA

View Full Version : design help: proxying for filtering, sorting, and structure modifications



BreakBad
14th June 2012, 20:30
Greetings,

In my QSortFilterProxy model I have implemented the filterAcceptsRow and lessThan methods. A new requirement has creep'd in which requires that the view needs to display/move some of the indexes as child indexes of other nodes. Basically there is meta-data with each index's data that indicates which other node it needs to be a displayed as a child of.

Without going into a trial and error mode I am wondering: What is the correct chain of proxy's in which I would have to implement to do this correctly?

* note: the model is only sorted on instantiation, sorting is otherwise disabled.

From what I understand this is about the design I would need to implement:
view <- AbstractProxy(mapTo, mapFrom) <- SortFilterProxy(lessThan, filterAccepts) <- AbstractItemModel

Any suggestions or alternatives?

tyvm!

-BB

wysota
14th June 2012, 21:01
An alternative is to use just one proxy that will do both filtering and mapping. Sorting can be done in the base model using QAbstractItemModel::sort() so a sorting proxy is not required in your situation.

BreakBad
14th June 2012, 21:51
Simple is better than complex, thanks.

Added after 27 minutes:

Well, it appears sort() won't accommodate the method for sorting that I require (a 'hard' coded list for the sort order). So it appears I'll have to use the previously mentioned proxy stack.

wysota
14th June 2012, 22:26
sort() is a virtual method. You can implement it any way you want. Using a sort proxy just to sort once doesn't make much sense as QSortFilterProxyModel is rather an expensive object to use.

BreakBad
19th June 2012, 15:23
Follow up question:

Decided to use both the abstract and proxy layer, as I needed to filter and map. Now I'm running into difficulty passing the correct index from the views to other views.

Here is the hierarchy:
TreeView -> AbstractProxyModel -> SortFilterProxy -> AbstractItemModel <- SortFilterProxy <- TableView

I'm trying to set the root index of the tableview by passing the current index of the treeview, resulting in "index from wrong model passed to mapFromSource"

What is the correct method for achieving this?

Currently I'm trying:
tree_proxy_index = treeview.sourceModel().mapToSource(treeview.curren tIndex())
item_model_index = tree_proxy_model.sourceModel().mapToSource(proxy_i ndex())
table_proxy_index = table_proxy_model.mapFromSource(item_model_index) # warning occurs here
table_view.setRootIndex(table_proxy_index)


TYVM

wysota
19th June 2012, 15:38
What warning?

BreakBad
19th June 2012, 16:07
What warning?

"index from wrong model passed to mapFromSource"

Problem solved. I noticed the address in memory of my AbstractItemModel was different for each view. I had two entirely separate instantiations of the model because of the way my init()'s were setup.