[Interest] Sharing QItemSelectionModel with multiple views/proxy models

Murphy, Sean smurphy at walbro.com
Thu Sep 24 22:12:46 CEST 2020


What's the best way to share selections between views when you're dealing with different levels of models and proxy models between the separate views?

I've got the following setup:
-  myData: a data only class that holds the underlying values to be displayed
- dataModel: a model that operates directly on myData 
- proxyModel:  a proxy model that dataModel as source model
 - view1: a QTableView that uses dataModel for its model
 - view2: another QTableView that uses proxyModel for its model

I'm trying to get selections on one view to be replicated to the other, so I originally assumed I'd just share a single selection model between them, so I do roughly the following:
  QAbstractTableModel* dataModel = new QAbstractTableModel(this);
  QAbstractProxyModel* proxyModel = new QAbstractProxyModel(this);  
  QItemSelectionModel* sharedSelectionModel = new QItemSelectionModel(dataModel);

  view1-> setModel(dataModel);

  proxyModel->setSourceModel(dataModel);
  view2->setModel(proxyModel);
 
  view1->setSelectionModel(sharedSelectionModel);
  view2->setSelectionModel(sharedSelectionModel);

That last call, attempting to set the selection model of view2 to sharedSelectionModel, fails since view2's model is the proxyModel, but the sharedSelectionModel that I'm attempting to set as the selection model operates on the dataModel. I get the reason why, but not what the best approach is to get around it.

So how do I connect things up so that selections on view1 are propagate through the proxy model, and then the respective indices are selected in view2, and vice versa?

Sean 





This message has been scanned for malware by Forcepoint. www.forcepoint.com


More information about the Interest mailing list