Results 1 to 3 of 3

Thread: QTableWidget sorting

  1. #1
    Join Date
    Jan 2008
    Location
    Vancouver, Canada
    Posts
    54
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget sorting

    Hello, I am making a QTableWidget which is working nicely. I want the user to be able to sort each column, which is also working, but I don't like how it sorts columns with numbers. It will sort the numbers from 0 to 10 like this:
    0
    1
    10
    2
    3
    4
    5
    6
    7
    8
    9
    But I want it like this:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    enum Qt::SortOrder just seems to have AscendingOrder and DescendingOrder. Is there anyway to achieve what I want?

    Thanks!
    Last edited by jpn; 28th January 2008 at 19:41. Reason: missing [quote] tags

  2. The following user says thank you to abrou for this useful post:

    Raccoon29 (20th March 2008)

  3. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget sorting

    Set the data as numbers instead of as text:
    Qt Code:
    1. int i = 0;
    2. QTableWidgetItem* item = ...
    3. //item->setText(QString::number(i)); // <-- no
    4. item->setData(Qt::DisplayRole, i); // <-- yes
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. The following 3 users say thank you to jpn for this useful post:

    abrou (28th January 2008), mattc (3rd May 2009), neuronet (7th October 2014)

  5. #3
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    23
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget sorting

    I was facing a similar problem, just a bit more complex than this one.
    In a QTableWidget, in some columns I had simple numeric values and in others I wanted to show them with a " %" appended.
    e.g. "65.32" -----> "65.32 %"
    But when sorting I wanted the cells to be sorted numerically.

    Thus I subclassed the QTableWidgetItem and then implemented the "<" operator to sort numbers and not strings.

    Qt Code:
    1. class MyTableWidgetItem : public QTableWidgetItem
    2. {
    3. if(text().contains("%"))
    4. {
    5. QString str1 = text();
    6. str1.chop(2);
    7. QString str2 = other.text();
    8. str2.chop(2);
    9. return str1.toDouble() < str2.toDouble();
    10. }
    11. else
    12. {
    13. return text().toDouble() < other.text().toDouble();
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Then while populating the table I passed instances of my custom items instead of the generic ones.
    I learnt the basics for this technique from here.

Similar Threads

  1. QTableWidget Sorting Widgets!
    By VireX in forum Qt Programming
    Replies: 4
    Last Post: 14th April 2007, 01:08
  2. Sorting elements in QTableWidget
    By Elmo23x in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2007, 08:35
  3. Model sorting vs. selection
    By VlJE in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 16:46
  4. QTableWidget Sorting
    By mclark in forum Newbie
    Replies: 4
    Last Post: 29th September 2006, 22:34
  5. QTableWidget Sorting Multiple Selection
    By rhiacasta in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 21:05

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.