Hi,

I need some help with the use of the QSortFilterProxyModel. I have searched the documentation and here on the forum but haven't been able to find any similar issues.

This is what I want to do:

I have two table views that should visualize the same data. One is a filtered version of the other so it will not show all columns. I call them collapsedView and expandedView. Both are QTableView.

To accomplish this filtering functionality I have added a QSortFilterProxyModel between the sourceModel (of type QAbstractTableModel) and the collapsedView:

Qt Code:
  1. QTableView* collapsedView = new QTableView();
  2. QTableView* expandedView = new QTableView();
  3.  
  4. SourceModel* sourceModel = new SourceModel(); // SourceModel is derived from QAbstractTableModel
  5. FilterModel* filterModel = new FilterModel(); // FilterModel is derived from QSortFilterProxyModel
  6. filterModel->setSourceModel(sourceModel);
  7.  
  8. collapsedView->setModel(filterModel);
  9. expandedView->setModel(sourceModel);
To copy to clipboard, switch view to plain text mode 

This works fine and both the views behave as I want.
But I also want to be able to sort the tables by clicking on the column headers. And I also want both the views to be sorted the same way regardless in what view I click the header. If I sort the items in one view, the other view should rearrange its rows in the same way.

So I tried to add another QSortFilterProxyModel between the source model and the filter model that should handle the sorting for both the views:

Qt Code:
  1. SourceModel* sourceModel = new SourceModel();
  2. SortModel* sortModel = new SortModel(); // SortModel is derived from QSortFilterProxyModel
  3. sortModel->setSourceModel(sourceModel);
  4. FilterModel* filterModel = new FilterModel();
  5. filterModel->setSourceModel(sortModel);
  6.  
  7. collapsedView->setModel(filterModel);
  8. expandedView->setModel(sortModel);
To copy to clipboard, switch view to plain text mode 

I also reimplemented the lessThan-method in the sortModel.
I am now able to sort according to columns by clicking on the header, but only the view that I click in are sorted. When I switch to the other view, this one is not affected by the sort.
It seems that the sorting only affects the view that the sorting was called from. How can I get the other view to be aware that it should change it layout?
Tried to emit the layoutChanged signal, but it doesn't seem to help.

Is this the right approach? Should I implement the sort-function on the source model instead? Thought I wouldn't have to do that since there is this proxy model for sorting.

I'd be grateful for any input!