View Full Version : Synchronize sorting between tables

10th September 2012, 18:58

I'm currently working on a Qt Project with QTableViews and QAbstractTableModel subclasses.

There are 2 synchronized tables (same number of row, synchronized scrollbar, etc.) and we are working on adding sorting on tables.

I use a customer QSortFilterProxyModel implementation to filter rows and sort model elements.

Here is an example/demo:

I wish to automatically arrange rows position in the right table when the left table is sorted by column (and vice-versa).

Is there any signal/slot mechanism to modify mapping tables of QSortFilterModel?

We try to implement something like :

bool SortFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
return otherInstance->lessThanFromOther(left, right);
return QSortFilterProxyModel::lessThan(left, right);
But the result is not really conclusive, I think we could find something more simple...

Thanks for the help.

11th September 2012, 06:00
If a model is sorted, essentially you move rows here and there. The mapping is kept in QSortFilterModel implementation. Now if you want some other model to know of this change of rows, it should point to the proxy model rather than the main model. For the rows were moved in proxy model not the main model. In you case, I think there are two models.
There can be two way I can think of. One is to make one big model. Let it include all the columns, if one is to one mapping is possible(must be right). Then add a QSFPM on top of that which gives the rows. The model has the view which only looks at columns it is interested in, while on the right, the view sees what it is interested in. Or write two additional QSFPM for the two views. filter acts on the main QSFPM.
In the other way, you need to track the mappings yourself. Basically which row in left model points to which row in right. Calling less than of left with right modelindex won't help. To create mapping, you need to write the index and parent functions and introduce you struct Map* as a void* into modelindex. So suppose you have mapping in the right model. All you'll do in the right qsfpm is to get void* internalPointer, cast it to map*, use mapFromSource to get left QSFPM index and THEN call the lessthan function on left model.
Hope I was able to get my point through.