qt - SortRole in QSortFilterProxyModel is not set -


i create qtableview custom model , custom sortfilterproxymodel

issuetablemodel *issuemodel = new issuetablemodel(this->_repository->getissuelist()); issuetablesortfilterproxymodel *proxymodel = new issuetablesortfilterproxymodel(this); proxymodel->setsourcemodel(issuemodel); this->_ui->issuestable->setmodel(proxymodel); 

and in sortfilterproxymodel constructor:

issuetablesortfilterproxymodel::issuetablesortfilterproxymodel(qobject *parent) : qsortfilterproxymodel(parent) {     this->setsortrole(qt::userrole);     this->setfilterrole(qt::userrole); } 

with custom lessthan method in proxymodel. when data retrieved via model data method,

  • qt::displayrole
  • qt::decorationrole
  • qt::fontrole
  • qt::textalignmentrole
  • qt::backgroundrole
  • qt::foregroundrole
  • qt::checkstaterole
  • qt::sizehintrole

are called, not qt::userrole need output correct sorting data model item:

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 sorting/filtering             switch (index.column()) {                 [...]             }         default:             return qvariant();     } } 

so question is: why data method of model never called qt::userrole when sorting proxymodel?

solution:

i got data in lessthan method via:

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 second parameter of data method specifies role...

qvariant leftdata = sourcemodel()->data(left, qt::userrole); 

if reimplement lessthan need perform sorting yourself. setsortrole affects default lessthan implementation.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -