Results 1 to 7 of 7

Thread: Numbers are sorted wrong in QTableView/QSqlQueryModel setup

  1. #1
    Join Date
    Aug 2012
    Posts
    18
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Question Numbers are sorted wrong in QTableView/QSqlQueryModel setup

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Numbers are sorted wrong in QTableView/QSqlQueryModel setup

    This works fine, but it will not sort numerically if your "floating point numbers" are actually strings:
    Qt Code:
    1. #include <QApplication>
    2. #include <QTableView>
    3. #include <QSqlQueryModel>
    4. #include <QDebug>
    5. #include <QSqlDatabase>
    6. #include <QSqlQuery>
    7. #include <QSortFilterProxyModel>
    8.  
    9. bool createData()
    10. {
    11. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    12. db.setDatabaseName(":memory:");
    13. if (db.open()) {
    14. QSqlQuery query;
    15. query.exec("create table test (id int, name text, value real)");
    16. query.exec("insert into test values(101, 'Name 1', 0.3)");
    17. query.exec("insert into test values(102, 'Name 2', 0.34)");
    18. query.exec("insert into test values(103, 'Name 3', -0.1)");
    19. query.exec("insert into test values(104, 'Name 4', 4.0)");
    20. query.exec("insert into test values(105, 'Name 5', 0.11)");
    21. return true;
    22. }
    23.  
    24. return false;
    25. }
    26.  
    27. int main(int argc, char *argv[])
    28. {
    29. QApplication app(argc, argv);
    30. if (!createData())
    31. return 1;
    32. model.setQuery("select * from test");
    33. proxy.setSourceModel(&model);
    34. QTableView view;
    35. view.setSortingEnabled(true);
    36. view.setModel(&proxy);
    37. view.show();
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 12th January 2014 at 05:53.

  3. The following user says thank you to ChrisW67 for this useful post:

    antimatter (13th January 2014)

  4. #3
    Join Date
    Aug 2012
    Posts
    18
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Numbers are sorted wrong in QTableView/QSqlQueryModel setup

    You made my day: I found the problem. Those numbers were really formatted as strings. In my QSqlQueryModel subclass, I returned those floats like
    Qt Code:
    1. return "%.2f" % data.toFloat()[0] # (this is python)
    To copy to clipboard, switch view to plain text mode 
    in order to display all numbers with 2 digits after the decimal point. The variable "data" is just the result of the baseclass' data() method. Now I return the unformatted data and sorting works with floats, too!

    But is there a way to sort those numbers correctly and display them in some formatted way? I've got two other columns, that contain right-ascention and declination as floats, and I display them by formatting them into degree/hours notation (string):
    RA: 1.767798 = 6h 45m 08.97s
    DEC: -0.29175 = -16° 42' 57.8"

    I set my proxy model like this
    Qt Code:
    1. proxy.setSortRole(Qt.UserRole)
    To copy to clipboard, switch view to plain text mode 
    and return plain floats in my models data() method for this role, but then sorting doesn't work at all. Any idea, how I might achieve that kind of sorting/displaying?


    Andi

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Numbers are sorted wrong in QTableView/QSqlQueryModel setup

    Typically you would return a human friendly string from data() when the Qt::DisplayRole is requested, and the native value when Qt::EditRole is requested. Sorting on Qt::EditRole then has the desired effect, the table view shows the user the human friendly text, and a reasonable default editor is more likely to be created.

    Astronomy application?

  6. The following user says thank you to ChrisW67 for this useful post:

    antimatter (12th January 2014)

  7. #5
    Join Date
    Aug 2012
    Posts
    18
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Numbers are sorted wrong in QTableView/QSqlQueryModel setup

    That is fantastic! Thank you very much. Now the sorting works like it should be AND I have my desired formatting.
    But why does EditRole work here but not UserRole?

    Yes, kinda astronomy application. It's purpose is to reconstruct the illuminated flight path of meteors ("fireballs") from these kinds of all-sky, long-exposure images:

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Numbers are sorted wrong in QTableView/QSqlQueryModel setup

    I don't see a reason the code would not sort on Qt::UserRole the same as any other. Perhaps there is some other quirk at play.

    Have fun with the project.

  9. #7
    Join Date
    Aug 2012
    Posts
    18
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Numbers are sorted wrong in QTableView/QSqlQueryModel setup

    I found the quirk.
    When I set my proxy models sortRole to "UserRole", it will call my QSqlQueryModel subclass' data() method with that role.
    In there I call the base class' data() method with the parameters fist (will receive UserRole, too), and will get an invalid QVariant because of this little excerpt from the QSqlQueryModel::data() source code.
    Qt Code:
    1. if (role & ~(Qt::DisplayRole | Qt::EditRole))
    2. return v;
    To copy to clipboard, switch view to plain text mode 
    With that out of the way, I can indeed have some fun. Thanks all.

    Andi

Similar Threads

  1. Get data from QTableView sorted by column
    By qks1 in forum Qt Programming
    Replies: 0
    Last Post: 26th June 2013, 09:27
  2. representation of numbers in QTableView...
    By KillGabio in forum Newbie
    Replies: 4
    Last Post: 26th January 2012, 03:59
  3. reload qtableview after sorted
    By poporacer in forum Newbie
    Replies: 0
    Last Post: 18th April 2011, 21:07
  4. QTableView/QSqlQueryModel
    By norobro in forum Qt Programming
    Replies: 7
    Last Post: 15th February 2008, 21:52
  5. Currently sorted column in QTableView
    By borges in forum Newbie
    Replies: 1
    Last Post: 21st September 2007, 16:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.