Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 573
Marius Avatar asked Sep 25 '08 22:09

Marius


3 Answers

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.

like image 72
Caleb Huitt - cjhuitt Avatar answered Nov 05 '22 18:11

Caleb Huitt - cjhuitt


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?

like image 42
Henrik Hartz Avatar answered Nov 05 '22 16:11

Henrik Hartz


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...

like image 1
j_kubik Avatar answered Nov 05 '22 16:11

j_kubik