I have a QListView and QTableView both sharing a subclassed QSortFilterProxyModel on top of a custom source model. Sorting is enabled in the QTableView, how to share the same sorting result between two views?
Do I reimplement the sort() or lessThan() function of the QSortFilterProxyModel? If so, how should i reimplement them?
This is currently my code and it's not working:
MyModel *model = new QMyModel();
MySortFilterModel *proxy_model = new MySortFilterModel();
proxy_model->setSourceModel(model);
proxy_model->setDynamicSortFilter(true);
QListView *list = new QListView();
list->setModel(proxy_model);
QTableView *table = new QTableView();
table->setModel(proxy_model);
table->setSortingEnabled(true);
I have also tried the following:
QHeaderView *header = table->horizontalHeader();
header->setSortIndicator(0, Qt::AscendingOrder);
header->setSortIndicatorShown(true);
header->setSectionsClickable(true);
connect(header,SIGNAL(sectionClicked(int)),table,SLOT(sortByColumn(int)));
But when I sort the table by clicking the header, the table is updated but not the list, with both view having different items at different rows.
I would like to have a result such that when user clicks the horizontal header of the QTableView, the item data are sorted, meanwhile the same item data which are shared in the QListView are also being sorted.