Results 1 to 2 of 2

Thread: Trouble with sortItems coercion of text in QTableWidget

Threaded View

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

    Question Trouble with sortItems coercion of text in QTableWidget

    I am using Qt Creator 1.2.92 based on Qt 4.6.0 32-bit.

    I'd like to develop a private slot to check each item of a QTableWidget to ensure that the item is (1) a "number" and (2) that the number is on the range (0,1). In the case that either of these tests fail, I would like to set the item to some specified coerced value. The code I wrote to do so is as follows:

    Qt Code:
    1. void myProject::formatTimingTable()
    2. {
    3. int i;
    4. int j;
    5. int nrow = ui->timing_tableWidget->rowCount();
    6. int ncol = ui->timing_tableWidget->columnCount();
    7. QString val;
    8. QTableWidgetItem *item, *itemReplace;
    9.  
    10. for (i = 0; i < nrow; i++)
    11. {
    12. for (j = 0; j < ncol; j++)
    13. {
    14. item = ui->timing_tableWidget->item(i, j);
    15.  
    16. // ensure item is not NULL
    17. if ( !item )
    18. continue;
    19.  
    20. val = item->text();
    21.  
    22. if (val <= "0"){
    23. itemReplace = new QTableWidgetItem("0.0001");
    24. ui->timing_tableWidget->setItem(i, j, itemReplace);
    25. }
    26.  
    27. if (val >= "1"){
    28. itemReplace = new QTableWidgetItem("0.9999");
    29. ui->timing_tableWidget->setItem(i, j, itemReplace);
    30. }
    31. }
    32. }
    33.  
    34. ui->timing_tableWidget->sortItems(0, Qt::AscendingOrder);
    35. }
    To copy to clipboard, switch view to plain text mode 

    This scheme works but the sortItems function can sometimes coerce a "0.0001" entry to "0". For example,


    1. When I run the associated app and enter a "0" in the first row of the table I get the result as shown in the attached firstZeroEntry.jpg file. In this case, the scheme works as planned.
    2. If, however, I then enter a "0" in the second row of the table I get the result shown in the attached secondZeroEntry.jpg file. In this case, the item in the first row is set to "0".
    3. If I then enter "0.4" in the third row of the table, I get the result shown in the third attached thirdZeroEntry.jpg file. In this case, the first and second row contain the correct "0.0001" entry.


    I have confirmed that the issue lies with sortItems function because when I comment the corresponding sort line in the code each "0" entry is suitably replaced by "0.0001" regardless of the the entry/row in the table.

    While I'm not entirely enthused by this whole approach in general, I am a bit confused as to why sortItems is coercing entries as described. Am I missing something?

    Secondly, is there an easier way to do all this? Suggestions/comments welcome.

    Finally, I realize that ncol = 1 (always) in my case. I could adapt the code accordingly...but that is another issue.

    Thanks!
    Attached Images Attached Images

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Change QTableWidget header name or text
    By ricardo in forum Qt Programming
    Replies: 3
    Last Post: 14th May 2009, 11:37
  3. Layout long text in QTableWidget
    By Tamtam in forum Newbie
    Replies: 0
    Last Post: 13th February 2009, 15:09
  4. Trouble with cursor and selecting text in QTextEdit
    By R_Torsten in forum Qt Programming
    Replies: 3
    Last Post: 7th June 2008, 19:17
  5. QTableWidget setSpan text display problem
    By sureshbabu in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2008, 16:14

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.