[Interest] ** Caution Message may be spoofed ** Re: Sharing QItemSelectionModel with multiple views/proxy models

Murphy, Sean smurphy at walbro.com
Mon Sep 28 16:32:51 CEST 2020


> > I'm trying to get selections on one view to be replicated to the
> > other[...]> 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?
> 
> Use QAbstractProxyModel::mapToSource() and mapFromSource(), or
> mapSelectionToSource() and mapSelectionFromSource().
> 
> Eg:
> QObject::connect(
>     sourceSelectionModel,
>     &QItemSelectionModel::selectionChanged,
>     proxySelectionModel,
>     [&](const QItemSelection &set) {
>         QItemSelection newSet = proxyModel->mapSelectionFromSource(set);
>         proxySelectionModel->select(newSet,
>             QItemSelectionModel::ClearAndSelect);
>     });
> 
> Repeat for each of the signals for QItemSelectionModel changes that should be
> mirrored, for each selection model. You can save some overhead by tracking
> which view originated the change, but QItemSelectionModel will detect empty
> change sets.

Thanks for the response. I ended up doing roughly the same thing.

I should have been more clear in my original post. I had found what looked like 
the relevant signals/functions to do the mapping as you described above before I 
posted. I wanted to make sure that I wasn't missing something in the 
model/selection model/proxy model system that AUTOMATICALLY made these 
connections for you. It seemed to me like this would be a fairly common use case - 
having multiple views that have differing levels of proxies, all feeding off from a 
common underlying data model, and wanting selections to be shared among views, 
once they are filtered through the proxies.

Sean


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


More information about the Interest mailing list