I am creating a qtableview with a custom model and a custom sortfilterproxymodel
IssueTableModel *issueModel = new IssueTableModel(this->_repository->getIssueList()); IssueTableSortFilterProxyModel *proxyModel = new IssueTableSortFilterProxyModel(this); proxyModel->setSourceModel(issueModel); this->_ui->issuesTable->setModel(proxyModel);
and in the constructor of sortfilterproxymodel:
IssueTableSortFilterProxyModel::IssueTableSortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) { this->setSortRole(Qt::UserRole); this->setFilterRole(Qt::UserRole); }
using the special lessThan method in the proxy model. but when data is retrieved using the data model method, only
- Qt :: DisplayRole
- Qt :: DecorationRole
- Qt :: FontRole
- Qt :: TextAlignmentRole
- Qt :: BackgroundRole
- Qt :: ForegroundRole
- Qt :: CheckStateRole
- Qt :: SizeHintRole
are called, but not by Qt :: UserRole, which I need to display the correct sorting data for the model element:
QVariant IssueTableModel::data(const QModelIndex &index, int role) const switch (role) { case Qt::DecorationRole:
So the question is: why is the data method of the model never called with Qt :: UserRole when sorting the proxy model?
Decision:
I got data in lessThan method with:
bool IssueTableSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const { QVariant leftData = sourceModel()->data(left); QVariant rightData = sourceModel()->data(right); switch (leftData.type()) { case QVariant::Int: return leftData.toInt() < rightData.toInt(); case QVariant::String: return leftData.toString() < rightData.toString(); case QVariant::DateTime: return leftData.toDateTime() < rightData.toDateTime(); default: return false; } }
but did not set the second parameter of the data method, which defines the role ...
QVariant leftData = sourceModel()->data(left, Qt::UserRole);
source share