PDA

View Full Version : Sorting QTableWidget alters selectioin of cells



zarkzervo
7th January 2009, 08:53
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::SelectRows );

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.

zarkzervo
7th January 2009, 13:31
I made a quick demo program to show how to provoke this error:


//How to test:
// 1) Start application
// 2) select all cells
// 3) sort by any column
// The column will shortly reach 9 (or 0) and stay sorted, then it is time to sort descending or choose another column
// White unselected cells should appear as soon as the sorting starts to move cells around.

ZTEST::ZTEST(const char *server, QWidget* parent):
QMainWindow(parent),
counter(0)
{
setupUi(this);
tbl->setRowCount(0);
tbl->setColumnCount(6);

//fill with some numbers
for(int i = 0; i < 4; i++)
{
tbl->insertRow(i);
for(int j = 0; j < 6; j++)
{
tbl->setItem(i, j, new QTableWidgetItem(QString::number(i+10*j)));
}
}

tbl->setSortingEnabled(true);

QTimer::singleShot(4000, this, SLOT(timerReturn()));
}

void ZTEST::timerReturn()
{
// keep first column as "id"
// Modify the values on the first row only
for( int i = 1; i < 6; i++)
{
QTableWidgetItem* item = tbl->item(0,i);
// item->setText(QString::number(counter) + item->text());
item->setText(QString::number(rand()%10));
}
QTimer::singleShot(2000, this, SLOT(timerReturn()));
}



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)

zarkzervo
8th January 2009, 14:13
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.