Quote Originally Posted by nikhilqt View Post
I do not edit the contents of the table, I only click on the column header of the tableview, such that the sorting of Ascending and Descending happens.
This useful?
Qt Code:
  1. #include <QtGui>
  2. #include <QDebug>
  3. #include <QStandardItemModel>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.  
  8. QApplication app(argc, argv);
  9.  
  10. QStandardItemModel model(10, 2);
  11. model.setSortRole(Qt::EditRole);
  12. for (int row = 0; row < 10; ++row) {
  13. qreal value = row * 1.2345678;
  14. QString text;
  15. text.setNum(value, 'g', 10);
  16.  
  17. // Column with only a user readable DisplayRole
  18. QStandardItem *item1 = new QStandardItem(text);
  19. model.setItem(row, 0, item1);
  20.  
  21. // Column with sortable EditRole
  22. QStandardItem *item2 = new QStandardItem(text);
  23. item2->setData(value, Qt::EditRole);
  24. model.setItem(row, 1, item2);
  25. }
  26.  
  27. tv.setModel(&model);
  28. tv.setSortingEnabled(true);
  29. tv.horizontalHeader()->setSortIndicator(0, Qt::AscendingOrder);
  30. tv.horizontalHeader()->setSortIndicator(1, Qt::AscendingOrder);
  31. tv.show();
  32.  
  33. return app.exec();
  34. }
  35.  
  36. // vi: sw=4 ts=4 et
To copy to clipboard, switch view to plain text mode 
Column 1 sorts based on the string representation. Column 2 sorts numerically but displays in default format.