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:
SourceModel* sourceModel = new SourceModel(); // SourceModel is derived from QAbstractTableModel
FilterModel* filterModel = new FilterModel(); // FilterModel is derived from QSortFilterProxyModel
filterModel->setSourceModel(sourceModel);
collapsedView->setModel(filterModel);
expandedView->setModel(sourceModel);
QTableView* collapsedView = new QTableView();
QTableView* expandedView = new QTableView();
SourceModel* sourceModel = new SourceModel(); // SourceModel is derived from QAbstractTableModel
FilterModel* filterModel = new FilterModel(); // FilterModel is derived from QSortFilterProxyModel
filterModel->setSourceModel(sourceModel);
collapsedView->setModel(filterModel);
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:
SourceModel* sourceModel = new SourceModel();
SortModel* sortModel = new SortModel(); // SortModel is derived from QSortFilterProxyModel
sortModel->setSourceModel(sourceModel);
FilterModel* filterModel = new FilterModel();
filterModel->setSourceModel(sortModel);
collapsedView->setModel(filterModel);
expandedView->setModel(sortModel);
SourceModel* sourceModel = new SourceModel();
SortModel* sortModel = new SortModel(); // SortModel is derived from QSortFilterProxyModel
sortModel->setSourceModel(sourceModel);
FilterModel* filterModel = new FilterModel();
filterModel->setSourceModel(sortModel);
collapsedView->setModel(filterModel);
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!
Bookmarks