Sync selectionModel between two views where one view has a transposed Model of the other
-
Hello,
I have two views. The first one uses the 'original' model and the second one uses the transposed model. See the general structure below:
original_Model = new QAbstractTableModel(this); transposed_Model = new QTransposeProxyModel(this); transposed_Model->setSourceModel(original_Model); original_View= new QTableView(this); original_View->setModel(original_Model); transposed_View = new QTableView(this); transposed_View ->setModel(transposed_Model); m_viewTransposed->setSelectionModel(m_viewOriginal->selectionModel());
As far as I know, i need the SAME model in the views, that
m_viewTransposed->setSelectionModel(m_viewOriginal->selectionModel());
would work. My question now is what is the best approach to sync the two selectionModels betweeen the two views. It cant be that difficult, because I only need to switch the columns and rows. Do I have to rewrite QItemSelectionModel? (A general hint would be enough)Thank you in advance!
-
Easy solution:
KLinkItemSelectionModel
https://api.kde.org/frameworks/kitemmodels/html/classKLinkItemSelectionModel.html -
@VRonin
Purely because I am interested in this.I looked at that link's documentation, but not its source code. I do note that it explains:
Although multiple views can share the same QItemSelectionModel, the views then need to have the same source model.
which clarifies why distinct
QItemSelectionModel
s will be needed.If the OP does not want to import KDE code, for whatever reason, in the specific case of
QTransposeProxyModel
is it indeed correct that all he has to do is "I only need to switch the columns and rows.", which might be simple to write in a couple of lines himself, rather than the generic solution obviously offered by KDE? -
It's a bit of a pain in the 🍑 as you'd also have to handle changes in the source model (see
QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved
for example). Not worth the effort, if you ask me, when you can just snag high quality code (Stephen Kelly original author and the current maintainer is David Faure that also maintains the model/views module of Qt) and be done with 1 line -
Hello @JonB,
yes I dont want to import KDE code (only pure C++ and Qt).
Could you give me a hint what exactly I have to rewrite?I tested for example:
connect(original_View->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), transposed_View, SLOT(onSelectionChanged(const QModelIndex &, const QModelIndex &)));
And in on SelectionChanged I use something like:
void CTableWidgetView::onSelectionChanged(const QModelIndex & originalIndexSelected, const QModelIndex & originalIndexDeselected) { selectionModel()->setCurrentIndex(model()->index(originalIndexSelected.column(), originalIndexSelected.row()), QItemSelectionModel::Select); selectionModel()->setCurrentIndex(model()->index(originalIndexDeselected.column(), originalIndexDeselected.row()), QItemSelectionModel::Deselect); }
But this works only in one direction and only if i select only ONE cell.
Thank you!
-
@firen said in Sync selectionModel between two views where one view has a transposed Model of the other:
I dont want to import KDE code
You really prefer code posted by randos on a forum to just using classes tried and tested by a major organisation?
only pure C++ and Qt
KLinkItemSelectionModel
is "pure C++ and Qt" -
@VRonin it is for my work (and i am a beginner) and not sure if i am aloud to use that (cause of copyright etc.)
But in general i found another solution, because I "switch" between the views so it is always only one view visible.
So i just:
- reset the selectionmodel of the view which is going to be visible
- read the selectedIndexes of the view which is going to be hidden
- iterate over the QModelIndexList i got and set the selectionModel of the visible view
That seems ok for my needs
-