Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange QTableWidget behavior - not all cells populated after sorting followed by repopulating

Tags:

qt

qt4

I have a QTableWidget that I populate like this:

  // Clear the table
  this->topPatchesTableWidget->setRowCount(0);

  this->topPatchesTableWidget->setRowCount(numberToDisplay);
  for(unsigned int pairId = 0; pairId < numberToDisplay; ++pairId)
    {
    // Display patch match scores
    QTableWidgetItem* myLabel = new QTableWidgetItem;
    myLabel->setData(Qt::DisplayRole, myValues[pairId]);
    this->tableWidget->setItem(pairId, 0, myLabel);
    ... fill other columns ...
    }

(I have some other UI elements to set properties that compute the values in myValues). If I change the properties, recompute, and recreate the table, everything works as expected. If I sort the table by clicking on one of the headers, it sorts correctly. HOWEVER, if at this point (after sorting) I click my button again to recompute the values and recreate the table, the table is very broken. That is, many of the cells are empty, and the cells that are not empty don't seem to be in any particular order.

By adding a manual call to

this->tableWidget->sortByColumn(0, Qt::AscendingOrder);

at the beginning of my CreateTable function, everything works as expected, but of course the newly created table is sorted by column 0 rather than the column that was selected for the last sort.

Does anyone have any idea why things would go so wrong without the call to sortByColumn? (I tried to make a simple example but I can't replicate the problem in a demo program).

Thanks,

David

like image 248
David Doria Avatar asked Oct 31 '11 22:10

David Doria


3 Answers

I had a similar problem in python, if a column header was selected to enable sorting the cells after that column stopped populating.

I got around it by setting self.tableWidget.setSortingEnabled(False) at the beginning of the row add method, then setting it back to self.tableWidget.setSortingEnabled(True) at the end of the row add method. According to riverbank computing this is the officially recommended way to address this problem.

QTableWidget.setItem (self, int row, int column, QTableWidgetItem item)

Note that if sorting is enabled (see sortingEnabled) and column is the current sort column, the row will be moved to the sorted position determined by item.

If you want to set several items of a particular row (say, by calling setItem() in a loop), you may want to turn off sorting before doing so, and turn it back on afterwards; this will allow you to use the same row argument for all items in the same row (i.e. setItem() will not move the row).

See also item() and takeItem().

like image 71
Karnisov Avatar answered Oct 22 '22 09:10

Karnisov


This is an elaboration on Karnisov's answer, also with PyQt. Disabling sorting before doing any setItem()s also worked for me, but it caused the user's sort selection to be lost, so I did it like this, where t is a QTableWidget:

oldSort = t.horizontalHeader().sortIndicatorSection()
oldOrder = t.horizontalHeader().sortIndicatorOrder()
t.setSortingEnabled(False)

# make the changes

t.sortItems(oldSort, oldOrder)
t.setSortingEnabled(True)

Note that it is still possible for the sort to change slightly with this code. This appears to be because the sort that Qt is using here doesn't move any items that were already in order, so if we had a table like this:

NUMBER    LETTER    OTHER LETTER
     1         B               C
     2         G               Q
     3         A               Q

If we sort by NUMBER, then by OTHER LETTER, nothing changes. But if we sort by LETTER, then by OTHER LETTER, we get:

NUMBER    LETTER    OTHER LETTER
     1         B               C
     3         A               Q
     2         G               Q

So if we had previously sorted by LETTER, then OTHER LETTER, this code would only do a single sort by OTHER LETTER, and we'd end up with the second version of the table instead of the first (assuming that the order the elements were added to the table in matches NUMBER). In this spot in my code, all items are deleted and re-added to refresh the table, so this can sometimes cause noticeable changes in order.

like image 39
Soren Bjornstad Avatar answered Oct 22 '22 09:10

Soren Bjornstad


I had the same problem - always, when I wanted to insert new data, i buildet the header and setSortingEnabled(true). I guess the QTableWidget does not like several calls for setSortingEnabled(true). So I put it into the constructor of my MainWindown, and thats it!

like image 1
Helena Avatar answered Oct 22 '22 08:10

Helena