Results 1 to 4 of 4

Thread: Sync sorting of a QTableWidget to other QTableWidget

  1. #1
    Join Date
    Jul 2015
    Posts
    2
    Qt products
    Platforms
    MacOS X Windows

    Default Sync sorting of a QTableWidget to other QTableWidget

    Hi,

    I've two QTableWidgets that have rows related to each other (e.g. identical row count). I want to enable sorting of a selected column and keep the related rows in sync. What would be a good way of implementing this?

    The other QTableWidget is also related to an underlying datacontainer-class, which will also need to be synchronized. However I'm still considering, if I should try to get rid of the underlying datacontainer to keep things simpler.

    Thank you for your time!

    Edit: Oh I see now that I should propably be using the QTableView to begin with :/ At least with the other table.
    Last edited by Ro; 31st July 2015 at 10:36.

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sync sorting of a QTableWidget to other QTableWidget

    connect
    QHeaderView::sortIndicatorChanged ( int logicalIndex, Qt::SortOrder order ) [signal]
    signal to other table slot, you will get logical index form this signal then sort your 2nd table with that logical index.

    table2.sort( column , Qt::AscendingOrder );


    Sample:

    Qt Code:
    1. Widget::Widget(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Widget)
    4. {
    5. ui->setupUi(this);
    6.  
    7. ui->t1->setSortingEnabled(true); // t1 & t2 are QTableWidget's
    8. ui->t2->setSortingEnabled(true);
    9.  
    10.  
    11. connect(ui->t1->horizontalHeader(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)), this, SLOT(sortIndicatorChangedSlot(int,Qt::SortOrder)));
    12.  
    13. }
    14.  
    15. Widget::~Widget()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void Widget::sortIndicatorChangedSlot(int col, Qt::SortOrder sortType)
    21. {
    22. ui->t2->sortByColumn(col, sortType);
    23. }
    To copy to clipboard, switch view to plain text mode 
    Thanks :-)

  3. #3
    Join Date
    Jul 2015
    Posts
    2
    Qt products
    Platforms
    MacOS X Windows

    Default Re: Sync sorting of a QTableWidget to other QTableWidget

    Hmm, I think the rows get mixed up in that way. Maybe I was unclear, but I what I need is to have rows of two different tables sorted as if they would be in the same table. So if corresponding rows a and b have mutual index 0 in the tables A and B before sorting, the rows a and b also have an mutual index after sorting.

    I'm now thinking of adding a temporary column containing row indexes before sorting to the table which triggers the sorting to sort the other table, but I was hoping I could have used some qt property.

    Thanks for the reply anyway!

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,518
    Thanks
    252
    Thanked 720 Times in 710 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Sync sorting of a QTableWidget to other QTableWidget

    The solution posted by prasad_N assumes that the two tables are identical and that sorting on any column in the first table will sort the same column in the second table. It doesn't mix up rows, unless the contents of the two columns are different.

    It sounds like there is only one (or a limited number) of columns shared between the two tables, and that what you want is to ensure that when one table is sorted on one of the shared columns, the second table is also sorted the same way.

    You can use the method proposed by prasad_N to implement this as well, if you insert a column-to-column map (QMap< int, int>). When you set up your tables, you create the mapping such that the key (first integer) contains the column index from table 1, and the value (second integer) is the corresponding column from table 2. (If there is only one column in column, you don't need this map, just make note that column 3 in table 1 is column 5 in table 2 or whatever).

    When your slot is called, if the index that is passed in (the column in table 1 that was clicked) is in the map, then you sort on the value column in the same order. If it isn't in the map, then you ignore it.

    An alternative is to convert to a QTableView and use a model that contains all the data for all columns in the two tables. You sort your model instead of sorting the table widget. For each table view, use a QSortFilterProxyModel-based proxy to present only those columns from the model which are to be displayed in that table.

Similar Threads

  1. QTableWidget sorting
    By abrou in forum Newbie
    Replies: 2
    Last Post: 6th June 2012, 10:53
  2. QTableWidget and sorting like int
    By TomASS in forum Newbie
    Replies: 3
    Last Post: 18th February 2010, 14:54
  3. Sorting a qtablewidget......
    By reshma in forum Qt Programming
    Replies: 8
    Last Post: 23rd March 2009, 14:34
  4. QTableWidget sorting
    By losiem in forum Qt Programming
    Replies: 4
    Last Post: 14th July 2008, 20:20
  5. QTableWidget Sorting
    By mclark in forum Newbie
    Replies: 4
    Last Post: 29th September 2006, 22:34

Tags for this Thread

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.