Results 1 to 5 of 5

Thread: sortItems by double value in QTableWidget

  1. #1
    Join Date
    Nov 2009
    Posts
    20
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation sortItems by double value in QTableWidget

    I have the following code for formatting a QTableWidget. The sortItems method sorts alphabetically and not numerically. Is there an easy fix to this code to be able to do so? Please be explicit as I a quite new at Qt.

    Qt Code:
    1. void myClass::formatTable(QTableWidget *table, double minimum, double maximum)
    2. {
    3. double val;
    4. int i;
    5. int nrow = table->rowCount();
    6. table->setSortingEnabled(false);
    7.  
    8. for (i = 0; i < nrow; i++)
    9. {
    10. // obtain tableWidgetItem
    11. item = table->item(i, 0);
    12.  
    13. // ensure item is not NULL, otherwise the application will crash!
    14. if (!item)
    15. {
    16. continue;
    17. }
    18.  
    19. // obtain cell string and convert to double value
    20. val = item->text().toDouble();
    21.  
    22. // make sure entries like ".7" are converted to "0.7". otherwise, sorting will be incorrect
    23. item->setText(QString::number(val));
    24.  
    25. // TRIED THIS BUT DID NOT WORK
    26. // item->setData(Qt::DisplayRole, val);
    27.  
    28.  
    29. // check the low bound
    30. if (val <= minimum)
    31. {
    32. item->setText(QString::number(minimum));
    33. }
    34.  
    35. // check the high bound
    36. if (val >= maximum)
    37. {
    38. item->setText(QString::number(maximum));
    39. }
    40.  
    41. // justification
    42. item->setTextAlignment(Qt::AlignRight);
    43. }
    44.  
    45. table->sortItems(0, Qt::AscendingOrder);
    46. }
    To copy to clipboard, switch view to plain text mode 

  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: sortItems by double value in QTableWidget

    use QTableWidgetItem::setData() when filling your table. Then all will work out of the box.

  3. #3

    Default Re: sortItems by double value in QTableWidget

    item->setData(Qt:isplayRole, val); = item->setText(QString::number(val));

  4. #4
    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: sortItems by double value in QTableWidget

    Quote Originally Posted by Nothing View Post
    item->setData(Qt:isplayRole, val); = item->setText(QString::number(val));
    First please use the CODE tags or at least turn off the smilies. Then you probably intend to use == instead of = but apart from that both expressions are not the same! In both cases the same is shown but when it comes to sorting the table the former is sorted numerical and the later alphabetical.

  5. The following user says thank you to Lykurg for this useful post:

    wconstan (27th December 2009)

  6. #5
    Join Date
    Nov 2009
    Posts
    20
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: sortItems by double value in QTableWidget

    Thanks much Lykurg for your reply. That worked like a charm.
    Last edited by wconstan; 27th December 2009 at 19:21. Reason: spelling error

Similar Threads

  1. Double Click Capturing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2011, 15:12
  2. Replies: 1
    Last Post: 18th November 2009, 08:38
  3. Replies: 6
    Last Post: 5th June 2009, 10:38
  4. Scaling
    By AD in forum Qt Programming
    Replies: 16
    Last Post: 11th July 2008, 11:55
  5. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2007, 00:29

Tags for this Thread

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.