Search code examples
qtqt4

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


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


Solution

  • 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().