Results 1 to 8 of 8

Thread: How to sort QListWidget by numbers...

  1. #1
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to sort QListWidget by numbers...

    So here I created a QListWidget, put some QListWidgetItems in it, and turned the sorting on by QListWidget::setSortingEnabled(true). Then I got such a result: 1, 10, 2, 3(sort by string)... while I had expected 1, 2, 3, ..., 10 (sort by number)

    & then I searched for information through Google but only got methods of Qt3. So how to solve this problem in Qt 4.6.2?

    Thanx!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to sort QListWidget by numbers...

    Very easy, just reimplement the < operator in your custom QListWidgetItem

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to sort QListWidget by numbers...

    Use QListWidgetItem::setData() not the constructor to set your value. Then all will work like you expect it to work.

    EDIT: since we are in Newbie I am gentile:
    Qt Code:
    1. int yourIntValue = 123456;
    2. item->setData(Qt::DisplayRole, yourIntValue);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2011
    Posts
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to sort QListWidget by numbers...

    I have a similar but slightly more complex variation. I have the numbers 1 through 16, prefixed with the letter "P", i.e. P1 - P16. As far as I can see, there's no way to have the sort use the numeric value, but have the display use the corresponding string. Is that correct?

    Thank you,
    Doug

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to sort QListWidget by numbers...

    Yes, as long as QListWidget is used.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to sort QListWidget by numbers...

    As tbscope says above, reimplement the < operator and use QString::rightJustified() in your comparison statement.

  7. #7
    Join Date
    Nov 2017
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How to sort the number as a string by QList and QTableView

    Hello Guys,

    I am new in QT, Can you please provide me some solution to sort the integer as a string by QList or QTableView .

    I am using QTableView and QStandardItemModel, In this, I am passing the integer value but it converts in string like: QStandardItem *trackIDItem = new QStandardItem(QString::number(trackID));
    And As we know TableView provides by default true option for sorting but It does not do in ascending or descending order same as QList<QString> list; list<<"1"<<"2"<<"4"<<"50"<<"30"; qSort(list);
    Output should be : 1, 2, 4, 30, 50 but It does not give as expected same problem with QTableView.

    Please provide me some solution to sort out this problem.

    Thanks in Advance.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to sort the number as a string by QList and QTableView

    There are at least two possible solutions:

    1 - Derive a new class from QStandardItemModel and reimplement the data() method. Add a custom Qt::ItemDataRole (for example, use Qt::UserRole) and add a clause to handle this role to the data() method. When data() is called with any role except Qt:: UserRole, return the value from the base class QStandardItemModel::data() method. For Qt:: UserRole, return the *integer* value of the display string. Very important: When you set up your initial model, call QStandardItemModel::setSortRole() using Qt:: UserRole (or the role you chose) as the argument. For this solution, you can use QStandardItem as the items added to the model without changes.

    2 - Derive a new class from QStandardItem and reimplement the QStandardItem::operator<() method. In this method, compare the *integer* values of your display strings and return the appropriate Boolean result. When you add items to your model, you will create instances of this derived class instead of QStandardItem. For this method, you can use QStandardItemModel without changes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Sort a QListWidget with Qt::AscendingOrder
    By ouekah in forum Newbie
    Replies: 2
    Last Post: 2nd August 2010, 18:05
  2. How to sort a Qlist AND get a sort key?
    By agerlach in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2010, 19:44
  3. how to SORT table of numbers with QT
    By tommy in forum Qt Programming
    Replies: 4
    Last Post: 29th May 2009, 10:34
  4. Complex Numbers
    By Max Yaffe in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2007, 18:40
  5. Column with numbers
    By ederbs in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2006, 23:03

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.