View Full Version : how to synchronize navigation for two QTableView's...

10th October 2007, 13:06
Hello :)
I have two QTableViews with equal number of rows connected with two different QStandardItemModel's. I'd like to navigate by one QTableView - and get the same selection on the other one. Now I do the following:

connect (tabview1, SIGNAL(pressed(const QModelIndex &)), tabview2, SLOT(setCurrentIndex(const QModelIndex &)));
connect (tabview2, SIGNAL(pressed(const QModelIndex &)), tabview1, SLOT(setCurrentIndex(const QModelIndex &)));
While I use the mouse everything is all right but when i begin to move by means of keyboard - there is no reaction in the second QTableView...certainly :(
How could I get the desired result? Maybe there is any other signal to use?
Thanks in advance...

10th October 2007, 13:11
You have to use the same selection model in both views, like this:

You don't need those connect statements. And remember that "current item" and "selected item" are different entities.

10th October 2007, 14:35
Thanks for answer. But I can't force it work :(

No result at all... Surely I make something wrong.

10th October 2007, 14:38
What do you mean by "no result"? What happens when you select an item in one of the views (I mean "select", so that its background changes)?

10th October 2007, 14:38
Two views can share same selection model only if they have same model as well. I'm afraid you need custom slots which in a way or other map QModelIndex belonging to one model in a form which is known to the other model.

10th October 2007, 14:52
Right. I missed that these are two different models.

10th October 2007, 14:57
What happens when you select an item in one of the views
Nothing happens :( The reason is that, probably

Two views can share same selection model only if they have same model as well

10th October 2007, 15:10
Maybe I can simply use any custom signal for work with keyboard events, analogous code in the first message? Before I use QTableWidget's and signal currentCellChanged(int,int,int,int) for this purpose but then some reasons appeares to replace these controls.

10th October 2007, 15:24
Can't you just use the same model in both views? What do they contain?

10th October 2007, 15:33
Unfortunately not. They contain different data. My purpose is just compare this data and find difference.

10th October 2007, 15:39
In what way find the difference? Items that exist in one model but don't exist in the other or items that have different data in the same position? Either way you have to connect to the itemSelectionChanged() signal and compare the selection with the other model manually.

10th October 2007, 15:54
All the comparisons have been made before, and these models just contain the flag of comparing result. I've noticed this defect at the last moment. :)
There is a decision - maybe not very good but it works!

connect(tabview1->selectionModel(), SIGNAL(currentChanged(const QModelIndex &,const QModelIndex &)), tabview2, SLOT(setCurrentIndex(const QModelIndex &)));
connect(tabview2->selectionModel(), SIGNAL(currentChanged(const QModelIndex &,const QModelIndex &)), tabview1, SLOT(setCurrentIndex(const QModelIndex &)));
A lot of thanks, everybody!

6th October 2009, 11:27

I would like to do something similar so I believe you can help me.

I have 4 treeview using the same QAbstractItemModel but different QSortProxyModel.
It is possible the share the selection between them?

I tried to use the same selection model for all of them by doing:

QItemSelectionModel* selectionModel = treeView1->selectionModel();

But I get the following error message when launching the application:
QAbstractItemView::setSelectionModel() failed: Trying to set a selection model, which works on a different model than the view.

I also tried to connect the tables selection models as shown in the previous post but nothing happens.

I could probably use a custom SLOT on itemSelectionChanged() SIGNAL and update the selection but I believe it would cycle?

Thanks for your help