How do I get a selection model to work with a proxy model?
Asked Answered
S

3

10

I have a model and two views set up like this:

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

When the user selects something in one of the views, I want the other view to mirror that selection. So I thought I'd use a QSelectionModel to link them together. But this does not work. I have a feeling it is because the views think they have two different models, when in fact they have the same model. Is there a way to get this to work?

Storfer answered 25/9, 2008 at 22:35 Comment(0)
D
2

What is probably happening is that the views do have two different models. One is your original model, the other is the sort filter model.

I'm not sure if this would work, and it depends on what Qt considers "activated", but you could connect a function to each of the view's activated slots. These will pass you a model index. You'll have to send the model index through the proxy model in the appropriate direction (mapFromSource and mapToSource). Then, call the setCurrentIndex on the other view.

The documentation for the activated signal states that what is considered "activated" varies by platform. There might be other signals you could latch onto, such as the selection model's selection changed signal. You might have to do a different call to change the selection as seen by the user. And finally, it might be possible or even easier to do in a derived QSelectionModel, as long as you remember about mapping to/from the source model.

Discography answered 25/9, 2008 at 23:44 Comment(0)
G
1

Not quite sure how your model subclass is implemented - but the selection depends on persistent model indexes being correct. Can you provide some source code? Are you using the same selection model on both?

Gauthier answered 14/1, 2009 at 20:45 Comment(0)
A
1

You propably need to use void QItemSelectionModel::select combined with QAbstractProxyModel::mapSelectionFromSource and QAbstractProxyModel::mapSelectionToSource. In QListView's selectionChange signal handler you should have

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

and analogically with mapSelectionFromSource in QTableView's signalChange signal handler.

Note that i am not sure if Qt will prevent infinite recursion when table will change selection of list which in turn will change selection of table and so on...

Adhere answered 19/9, 2011 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.