Results 1 to 3 of 3

Thread: Sorting QTableWidget alters selectioin of cells

  1. #1
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Sorting QTableWidget alters selectioin of cells

    I have a QTableWidget containing 10 columns.
    I have an object myCar *Car which keeps a pointer to the QTableWidgetItems in a row:
    QTableWidgetItem *item_id;
    QTableWidgetItem *item_depth;
    QTableWidgetItem *item_angle; and so on...

    I then select a cell containing the item_depth (this works).
    I can sort on any column and it still works.
    I then start updating the data in several cells like this:
    item_depth->setText(QString::number(car->depth()));
    (all but the ID are frequently updated)
    The selection still works and follows the Car object up and down the table.
    I can select 5 cells on the same row (belonging to the Car object) and the selection keeps to the same row.
    Then I select the sixth cell on the same row. The next update, suddenly one of the selections 'jumps' to another row. The data on each row are correct, but the selection is not keeping to the correct row. Some of them are sticking to the original row, but one of the selections have jumped to a different row and stays there.

    Some times this anomality begins at the 3rd selected and other times 4th, so there is no "6th time consistency".
    Also if I sort on item_id, this does not occur because then the rows are not moved.

    I discovered the error as I had enabled selection of whole rows of data:
    tbl_cars->setSelectionBehavior(QAbstractItemView::SelectRow s);

    Any suggestions on what i should do?
    I have added all QTableWidgetItems to the table and only used setText() on them to change the text, i.e., I have not added more cells/rows as I know sorting should be turned off before addition.

    I do turn of sorting before adding a row and turn it on afterwards.

  2. #2
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sorting QTableWidget alters selectioin of cells

    I made a quick demo program to show how to provoke this error:
    Qt Code:
    1. //How to test:
    2. // 1) Start application
    3. // 2) select all cells
    4. // 3) sort by any column
    5. // The column will shortly reach 9 (or 0) and stay sorted, then it is time to sort descending or choose another column
    6. // White unselected cells should appear as soon as the sorting starts to move cells around.
    7.  
    8. ZTEST::ZTEST(const char *server, QWidget* parent):
    9. QMainWindow(parent),
    10. counter(0)
    11. {
    12. setupUi(this);
    13. tbl->setRowCount(0);
    14. tbl->setColumnCount(6);
    15.  
    16. //fill with some numbers
    17. for(int i = 0; i < 4; i++)
    18. {
    19. tbl->insertRow(i);
    20. for(int j = 0; j < 6; j++)
    21. {
    22. tbl->setItem(i, j, new QTableWidgetItem(QString::number(i+10*j)));
    23. }
    24. }
    25.  
    26. tbl->setSortingEnabled(true);
    27.  
    28. QTimer::singleShot(4000, this, SLOT(timerReturn()));
    29. }
    30.  
    31. void ZTEST::timerReturn()
    32. {
    33. // keep first column as "id"
    34. // Modify the values on the first row only
    35. for( int i = 1; i < 6; i++)
    36. {
    37. QTableWidgetItem* item = tbl->item(0,i);
    38. // item->setText(QString::number(counter) + item->text());
    39. item->setText(QString::number(rand()%10));
    40. }
    41. QTimer::singleShot(2000, this, SLOT(timerReturn()));
    42. }
    To copy to clipboard, switch view to plain text mode 

    I can't see that I've done any obvious mistakes as I never "steal" the QTableWidgetItems.

    I'm using Qt 4.4.3 for Linux and gcc (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)

  3. #3
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sorting QTableWidget alters selectioin of cells

    I posted a bug report to Trolltech and got this answer:
    Thank you for reporting this issue. I was able to reproduce this issue
    with Qt 4.4.3 as reported. I tested this same problem with the the
    current 4.5.0 beta/snapshots and also the unreleased 4.4.x snapshots
    and both have this error fixed.

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.