The bottom line is to manually sort between sorting by -1 column (recovery) and the normal column number and somehow intercept the connection between QHeaderView
and QSortFilterProxyModel
.
So, using some insight from @ vahancho's answer, I managed to sort as follows:
class ProxyModel : public QSortFilterProxyModel { Q_OBJECT public: ProxyModel(QObject* parent = 0); signals: void askOrder(int column, Qt::SortOrder order); public slots:
and on the parent side I made the correct connection and checked:
ResultsTable::ResultsTable(QWidget *parent) : QTableView(parent) { p_Header = new QHeaderView(this); p_Sort = new ProxyModel(this); connect(this, &ResultsTable::doSort, p_Sort, &ProxyModel::doSort); connect(p_Sort, &ProxyModel::askOrder, this, &ResultsTable::setSorting); setSortingEnabled(true); } void ResultsTable::setSorting(int column, Qt::SortOrder order) { if (p_Header->sortIndicatorOrder() == Qt::AscendingOrder && p_Header->isSortIndicatorShown() && m_PreviousSort == column) { p_Header->setSortIndicator(column, Qt::DescendingOrder); p_Header->setSortIndicatorShown(false); column = -1; } else { p_Header->setSortIndicatorShown(true); } m_PreviousSort = column; emit doSort(column, order); }
That way, I can use the automatic sorting performed by QTableView
when sortingEnabled
is true
. I tried to investigate what happens inside Qt when the table header is clicked to cause sorting, but it failed, so I stopped with this solution.
I'm still not sure if it is correct that the QTableView
is responsible for setting the correct sort, and not the QHeaderView
itself (since I thought this functionality should belong to the header).
source share