Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can i sort an alphanumeric string in abstract table view
Forum Update on Monday, May 27th 2025

How can i sort an alphanumeric string in abstract table view

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manikandan
    wrote on 26 May 2016, 09:39 last edited by A Former User
    #1

    Hi, am new to QT. I am using a QAbstractTableModel for displaying some file information. One of the column contains size of the file. If the size is maintained as double[without any unit like Kb, Mb, Gb] then sorting works fine with the following code

    class MyModel : public QAbstractTableModel   
    {
    ...
    ...
    }
    
    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel( myModel );
    ui->tableView->setModel( proxyModel );
    

    But when i try to maintain the file size in human readable format[with units Kb, Mb, Gb] then i changed double to QString and then sorting is not in proper order. I know that sorting of strings work based on ASCII values but how can i achieve proper sorting for this case?

    PS: I tried implementing lessthan function on my own, but faced ambiguity error because of QAbstractTableModel and QSortFilterProxyModel

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 26 May 2016, 14:26 last edited by Chris Kawa
      #2

      Hi, welcome to the forum.

      No need to implement string sorting. It will be slow and is unnecessary. This is a good use case for user roles in a model.
      Make the model return a decorated string value for Qt::DisplayRole and the original double value for some custom role (e.g. Qt::UserRole). Then set the sorting role in the proxy model like this: proxyModel->setSortRole(Qt::UserRole); and you should be good to go. Displayed will be the string but for sorting the original double value will be used.

      Btw. surround code with ``` to make it format better (I corrected your post).

      1 Reply Last reply
      2
      • M Offline
        M Offline
        manikandan
        wrote on 27 May 2016, 09:37 last edited by
        #3

        Thank you Chris...It works :)

        1 Reply Last reply
        0

        1/3

        26 May 2016, 09:39

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved