View Full Version : Sync sorting of a QTableWidget to other QTableWidget

31st July 2015, 08:36

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.

31st July 2015, 12:45
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 );


Widget::Widget(QWidget *parent) :
ui(new Ui::Widget)

ui->t1->setSortingEnabled(true); // t1 & t2 are QTableWidget's

connect(ui->t1->horizontalHeader(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)), this, SLOT(sortIndicatorChangedSlot(int,Qt::SortOrder))) ;


delete ui;

void Widget::sortIndicatorChangedSlot(int col, Qt::SortOrder sortType)
ui->t2->sortByColumn(col, sortType);

10th August 2015, 12:03
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!

10th August 2015, 23:26
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.