I have the following code for formatting a QTableWidget. The sortItems method sorts alphabetically and not numerically. Is there an easy fix to this code to be able to do so? Please be explicit as I a quite new at Qt.

Qt Code:
  1. void myClass::formatTable(QTableWidget *table, double minimum, double maximum)
  2. {
  3. double val;
  4. int i;
  5. int nrow = table->rowCount();
  6. table->setSortingEnabled(false);
  7.  
  8. for (i = 0; i < nrow; i++)
  9. {
  10. // obtain tableWidgetItem
  11. item = table->item(i, 0);
  12.  
  13. // ensure item is not NULL, otherwise the application will crash!
  14. if (!item)
  15. {
  16. continue;
  17. }
  18.  
  19. // obtain cell string and convert to double value
  20. val = item->text().toDouble();
  21.  
  22. // make sure entries like ".7" are converted to "0.7". otherwise, sorting will be incorrect
  23. item->setText(QString::number(val));
  24.  
  25. // TRIED THIS BUT DID NOT WORK
  26. // item->setData(Qt::DisplayRole, val);
  27.  
  28.  
  29. // check the low bound
  30. if (val <= minimum)
  31. {
  32. item->setText(QString::number(minimum));
  33. }
  34.  
  35. // check the high bound
  36. if (val >= maximum)
  37. {
  38. item->setText(QString::number(maximum));
  39. }
  40.  
  41. // justification
  42. item->setTextAlignment(Qt::AlignRight);
  43. }
  44.  
  45. table->sortItems(0, Qt::AscendingOrder);
  46. }
To copy to clipboard, switch view to plain text mode