PDA

View Full Version : Showing selected rows in a separate table



dnnc
20th June 2007, 18:04
Hi,

I would like to have 2 separate tables, one showing all the items, and the other one the current selection of the original items.

My current solution is as follows; I create a proxy of the original model, to which I also pass its item selection model. I overwrite the QSortFilterProxyModel::filterAcceptRow() function, and return true only if the row is selected. In order to ensure that the second table is updated each time that the selection is changed, I connect the signal selectionChanged which calles a wrapper slot that just invokes QSortFilterProxyModel::filterChanged(). I attach the minimal code that implements what I described above and a screenshot showing the desired behavior.

Although it works, I don't like this solution for the following reasons:
1) filterAcceptRow uses the info from the selection model and not from the source model to decide whether a row should be filtered or not, which may not be the neatest solution
2) filterChanged() exists only from Qt4.2 and I need my application to be compatible with Qt4.1 too
3) if I understand well, filterChanged() recalculates accepted rows for all the indexes of the source models, and not only the ones which were removed from/added to a selection. Given that I can have tens of thousands of rows in the original model, this does not look like an optimal solution.

Does anyone have a better idea of implementing such functionality?

dnnc
21st June 2007, 14:16
Hello, isn't there any model/view expert willing to help? Any suggestion would be greatly appreciated!

Thanks

jpn
21st June 2007, 15:23
One option could be to wrap the QItemSelectionModel into a QAbstractListModel. This way the model would operate on the selected indexes only (whereas the proxy operates on the whole source model).

dnnc
21st June 2007, 17:35
Thanks a lot for your hint! Here is what I did, I created a class ProxyModel which extends QAbstractListModel and takes the source model and the item selection model as arguments. The proxy model contains the list of selected indexes from the source model. I connect the QItemSelectionModel::selectionChanged signal to a slot which updates the Proxy Model, ie. removes the indexes which were diselected and appends indexes which were selected. I also overwrite the data() method in the Proxy Model so that it returns data from the source model.

You can find a working implementation attached, there might be some additional checkings/optimizations to be done, but it shows the idea. Comments are welcome.