How to get a selection model for working with a proxy model?

I have a model and two views configured as follows:

Model ---> OSortFilterProxyModel ---> OListView Model ------------------------------> OTableView 

When a user selects something in one of the views, I want the other view to display that selection. So I decided to use QSelectionModel to combine them. But that does not work. I have a feeling because the views think that they have two different models, when in fact they have the same model. Is there any way to make this work?

+6
model-view-controller qt selectionmodel
source share
3 answers

What is probably happening is that the views have two different models. The first is your original model, the other is a sorting filter model.

I'm not sure if this will work, and it depends on what Qt considers “activated,” but you can connect a function to each of the active slots. They will give you a model index. You will need to send the model index through the proxy model in the appropriate direction (mapFromSource and mapToSource). Then call setCurrentIndex on another view.

The documentation for the activated signal indicates that what is considered “activated” is platform dependent. There may be other signals that you could click into, for example, selecting the selected selection model. You may need to make another call to change the selection as seen by the user. And finally, it may be possible or even easier to do in a derived QSelectionModel if you remember about matching with / from the original model.

+2
source share

Not quite sure how your subclass of the model is implemented, but the choice depends on the correctness of the indexes of the constant models. Can you provide some kind of source code? Do you use the same selection model on both?

+1
source share

You are comfortable using void QItemSelectionModel :: select in combination with QAbstractProxyModel :: mapSelectionFromSource and QAbstractProxyModel :: mapSelectionToSource . In a QListView selectionChange signal handler you must have

 tableView->selection()->select( proxyModel->mapSelectionToSource(selected), QItemSelectionModel::ClearAndSelect); 

and similar to mapSelectionFromSource in the QTableView signalChange signal handler.

Note that I'm not sure if Qt will prevent infinite recursion when the table changes the list selection, which in turn changes the table selection and so on ...

+1
source share

All Articles