Results 1 to 3 of 3

Thread: QTableWidget sorting question

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget sorting question

    I'm sorting a QTableWidget object with the sortItems() function.

    Qt Code:
    1. QTableWidget myTable = new QTableWidget(rows, columns, this);
    2. myTable->sortItems(1, Qt::AscendingOrder);
    To copy to clipboard, switch view to plain text mode 

    Everything works fine but ascendingly rows are sorted like this:
    1,.........................
    10,......................
    2,.......................
    3.......................
    etc.

    In other words, this sort Items function thinks that 2>10 because 10 starts with a 1. How could I avoid this and make it consider number 10 as something that's bigger than 9 and smaller than 11.
    This problem may have something to do with how I load myTable with integers. This is how it's done (do you see a problem with it):

    Qt Code:
    1. QTableWidgetItem *item = new QTableWidgetItem(QString::number(integer));
    2. myTable->setItem(y, x, item);
    To copy to clipboard, switch view to plain text mode 

    Later the sorted integers are extracted from myTable like this:
    Qt Code:
    1. QTableWidgetItem *a = myTable->item(y,x);
    2. int val = a->text().toInt();
    To copy to clipboard, switch view to plain text mode 

    Thank you!

  2. #2
    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: QTableWidget sorting question

    for that you better use
    Qt Code:
    1. item->setData(Qt::DisplayRole, 10);
    To copy to clipboard, switch view to plain text mode 
    instead of setting the value on the ctor. Then your items will get sorted like integers.

  3. The following 2 users say thank you to Lykurg for this useful post:

    Bing (28th July 2009), tommy (31st May 2009)

  4. #3
    Join Date
    Oct 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget sorting question

    How do I sort a table if I use qreal as data type in one of the columns and wishes to sort on that column?
    Last edited by codemonkey; 3rd October 2009 at 13:12.

Similar Threads

  1. Sorting QTableWidget alters selectioin of cells
    By zarkzervo in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2009, 14:13
  2. QTableWidget sorting
    By losiem in forum Qt Programming
    Replies: 4
    Last Post: 14th July 2008, 20:20
  3. QTableWidget sorting with hidden rows
    By alex140773 in forum Qt Programming
    Replies: 0
    Last Post: 8th July 2008, 12:35
  4. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  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
  •  
Qt is a trademark of The Qt Company.