PDA

View Full Version : Show selected rows



mpi
16th March 2010, 20:37
Dear Gurus,

Suppose I have a table model that I view in a QTableView (A). In another QTableView (B) I want to display the rows that are selected in the first QTableView (A).

How do I achieve this? Ie, each selected row in view (A) should be displayed in view (B).

Best regards,

Mads

waynew
16th March 2010, 21:50
I haven't tried this yet (but I need to, since I need similar code too), but I would try this approach:

Use viewA>selectedIndexes() to return a QModelIndexList
Then a QFilterProxyModel to translate the viewA indexes to the modelA indexes.
Then get the selected data with modelA->data() - returns QVariant
Then insert the data into modelB using insertRows
Then modelB->select()

Good luck!

waynew
16th March 2010, 22:26
Also, check out QItemSelectionModel

stefanadelbert
23rd June 2010, 01:59
You shouldn't need to have two models as the underlying data is the same for both TreeViews. This is one of the massive benefits of the MV(C) implementation of the Qt Model/View framework. It should definitely be possible to feed selected model indexes from one view to another using a QSortFilterProxy on the second to filter out the indexes that aren't required or rather filter in the nidexes that are required.

I imagine you would start by getting the selected indexes of the treeViewA using a QItemSelectionModel:


QItemSelectionModel* selectionModel = tableViewA->selectionModel();

Then handle treeViewA::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected) which will be emitted whenever the selection in treeViewA changes (obviously!) and do one of these:


QModelIndexList indexList = selectionModel->selectedIndexes()
// OR
QModelIndexList indexList = selectionModel->selectedRows()
// OR
QModelIndexList indexList = selectionModel->selectedColums()

Problem comes in knowing what to do with that list of selected indexes. I was hoping that you would be able to somehow get a QSortFilterProxyModel to filter based on a collection of indexes. I've just had a look at the documentation and it's not obvious how to do that. If I come up with anything, I'll post.