Hello,

I've got a QTableView / QSortFilterProxyModel / QSqlQueryModel-subclass setup. Some columns contain floating point numbers and the default sorting is by alphabet. That will sort numbers the wrong way like this:
Qt Code:
  1. -0.06
  2. -1.45
  3. 0.04
  4. 0.15
To copy to clipboard, switch view to plain text mode 
Of course, -1.45 is less than -0.06 and should occur before -0.06.

So, how do I make the model(s) sort the right way? I researched quite a while, but the more I read the more I get confused. I read about several possible approaches, but they don't work, shouldn't be used or I don't understand them.

  • SQL model should be left alone, sorting should be implemented in proxy model. I don't know how. Reimplementing lessThan() is my working solution, but apparently it is discouraged.
  • Use a custom SortRole with the proxy model and return actual floats in SQL model subclass for this role. Didn't work. Custom role didn't arrive in the model.
  • Reimplement sort() in the SQL model subclass. Don't know how, because the data is handled internally by some QSqlQueryModelPrivate class. I might reset the QSqlQuery with a new query containing a order-by clause. But that will fetch the data from the database again. Seems to be strange, as the data already resides in that QSqlQueryModelPrivate class instance.


I was hoping, someone could show me the 'proper' way for sorting floating point numbers.

All the best,
Andreas