Results 1 to 4 of 4

Thread: Sorting elements in QTableWidget

  1. #1
    Join Date
    Jan 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Sorting elements in QTableWidget

    Hi to all!

    I want to sort some elements in a QTableWidget.... the problem is that I do not know how to do it with a colum with numbers in a correct way. Now the result is something like this:

    1
    10
    11
    12
    2
    20
    21
    3
    4
    5
    6

    and I would like to have:

    1
    2
    3
    4
    5
    6
    10
    11
    12
    20
    21


    I hope that you can understand what I mean Thanks for your help!!!

  2. #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: Sorting elements in QTableWidget

    Either set the numbers as integers, not as text or provide a custom QTableWidgetItem::operator<().


    Approach 1:
    Qt Code:
    1. int i = 0;
    2. QTableWidgetItem* item = ...;
    3. item->setText(QString::number(i)); // don't use this
    4. item->setData(Qt::DisplayRole, i); // but this
    To copy to clipboard, switch view to plain text mode 

    Approach 2:
    Subclass QTableWidgetItem, override QTableWidgetItem::operator<().
    J-P Nurmi

  3. #3
    Join Date
    Feb 2006
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting elements in QTableWidget

    Qt Code:
    1. void sortAsc()
    2. {
    3. TableWidget->sortItems ( TableWidget->currentColumn() , Qt::AscendingOrder );
    4. }
    5. void sortDesc()
    6.  
    7. {
    8. TableWidget->sortItems ( TableWidget->currentColumn() , Qt::DescendingOrder );
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Sorting elements in QTableWidget

    Quote Originally Posted by crocus View Post
    Qt Code:
    1. void sortAsc()
    2. {
    3. TableWidget->sortItems ( TableWidget->currentColumn() , Qt::AscendingOrder );
    4. }
    5. void sortDesc()
    6.  
    7. {
    8. TableWidget->sortItems ( TableWidget->currentColumn() , Qt::DescendingOrder );
    9. }
    To copy to clipboard, switch view to plain text mode 
    This does not make any difference because the items are in ascending order. They just need to be interpreted and sorted as numbers, not as text.
    J-P Nurmi

Similar Threads

  1. Model sorting vs. selection
    By VlJE in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 16:46
  2. QTableWidget Sorting
    By mclark in forum Newbie
    Replies: 4
    Last Post: 29th September 2006, 22:34
  3. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  4. QTableWidget Sorting Multiple Selection
    By rhiacasta in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 21:05
  5. Replies: 6
    Last Post: 5th March 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.