SortRole in QSortFilterProxyModel not installed

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: // Display icons switch (index.column()) { [...] } case Qt::DisplayRole: // Display text data switch (index.column()) { [...] } case Qt::UserRole: qDebug() << "USER ROLE!!!!"; // Return data for sorting/filtering switch (index.column()) { [...] } default: return QVariant(); } } 

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); 
+4
source share
2 answers

If you override lessThan , you need to do the sorting yourself. setSortRole only affects the default implementation of lessThan .

+4
source

You must either:

call

 void QSortFilterProxyModel::sort ( int column, Qt::SortOrder order = Qt::AscendingOrder ) [virtual] 

remapping in your view (and click columns)

 setSortingEnabled(true); 

Set dynamic filtering

 void QSortFilterProxyModel::setDynamicSortFilter ( bool enable ) 

Edit:

Please note that in the docs. They say in:

At this point, neither sorting nor filtering is enabled; The original data is displayed in the view. Any changes made to the QSortFilterProxyModel apply to the original model.

You MUST run filtering / sorting

0
source

Source: https://habr.com/ru/post/1495924/


All Articles