Results 1 to 13 of 13

Thread: how to synchronize navigation for two QTableView's...

  1. #1
    Join Date
    Jul 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default how to synchronize navigation for two QTableView's...

    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:
    Qt Code:
    1. connect (tabview1, SIGNAL(pressed(const QModelIndex &)), tabview2, SLOT(setCurrentIndex(const QModelIndex &)));
    2. connect (tabview2, SIGNAL(pressed(const QModelIndex &)), tabview1, SLOT(setCurrentIndex(const QModelIndex &)));
    To copy to clipboard, switch view to plain text mode 
    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...

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to synchronize navigation for two QTableView's...

    You have to use the same selection model in both views, like this:
    Qt Code:
    1. tabview2->setSelectionModel(tabview1->selectionModel());
    To copy to clipboard, switch view to plain text mode 
    You don't need those connect statements. And remember that "current item" and "selected item" are different entities.

  3. #3
    Join Date
    Jul 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to synchronize navigation for two QTableView's...

    Thanks for answer. But I can't force it work
    Qt Code:
    1. tabview1->setModel(Model1);
    2. tabview2->setModel(Model2);
    3. tabview1->setSelectionModel(tabview2->selectionModel());
    To copy to clipboard, switch view to plain text mode 
    No result at all... Surely I make something wrong.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to synchronize navigation for two QTableView's...

    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)?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to synchronize navigation for two QTableView's...

    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.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    Ursa (10th October 2007)

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to synchronize navigation for two QTableView's...

    Right. I missed that these are two different models.

  8. #7
    Join Date
    Jul 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to synchronize navigation for two QTableView's...

    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

  9. #8
    Join Date
    Jul 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to synchronize navigation for two QTableView's...

    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.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to synchronize navigation for two QTableView's...

    Can't you just use the same model in both views? What do they contain?

  11. The following user says thank you to wysota for this useful post:

    Ursa (10th October 2007)

  12. #10
    Join Date
    Jul 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to synchronize navigation for two QTableView's...

    Unfortunately not. They contain different data. My purpose is just compare this data and find difference.

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to synchronize navigation for two QTableView's...

    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.

  14. #12
    Join Date
    Jul 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to synchronize navigation for two QTableView's...

    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!
    Qt Code:
    1. connect(tabview1->selectionModel(), SIGNAL(currentChanged(const QModelIndex &,const QModelIndex &)), tabview2, SLOT(setCurrentIndex(const QModelIndex &)));
    2. connect(tabview2->selectionModel(), SIGNAL(currentChanged(const QModelIndex &,const QModelIndex &)), tabview1, SLOT(setCurrentIndex(const QModelIndex &)));
    To copy to clipboard, switch view to plain text mode 
    A lot of thanks, everybody!

  15. #13
    Join Date
    Aug 2009
    Posts
    23
    Thanks
    3

    Default Re: how to synchronize navigation for two QTableView's...

    Hi,

    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:

    Qt Code:
    1. QItemSelectionModel* selectionModel = treeView1->selectionModel();
    2. treeView2->setSelectionModel(selectionModel);
    3. treeView3->setSelectionModel(selectionModel);
    4. treeView4->setSelectionModel(selectionModel);
    To copy to clipboard, switch view to plain text mode 

    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
    Lionel

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.