Strange QTableWidget behavior - not all cells filled after sorting followed by refilling

I have a QTableWidget that I populate as follows:

// 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 user interface elements for setting properties that compute values ​​in myValues). If I change the properties, re-read and re-create the table, everything will work as expected. If I sort a table by clicking on one of the headers, it is sorted correctly. HOWEVER, if at the moment (after sorting) I press the button again to recalculate the values ​​and recreate the table, the table is very broken. That is, many of the cells are empty, and cells that are not empty do not look in a specific order.

Adding a manual call

 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, not the column that was selected for the last sort.

Does anyone have any idea why everything would be so wrong without calling sortByColumn? (I tried to make a simple example, but I cannot reproduce the problem in a demo program).

Thanks,

David

+6
source share
4 answers

I had a similar problem in python if a column heading was selected to enable cell sorting after that column no longer populated.

I walked around it by setting self.tableWidget.setSortingEnabled (False) at the beginning of the add row method, and then returning it to self.tableWidget.setSortingEnabled (True) at the end of the add row method. According to calculations by the river, this is the officially recommended way to solve this problem.

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

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

If you want to set several elements of a certain string (for example, by calling setItem () in a loop), you can turn off sorting before doing this, and then turn it back on; this will allow you to use the same row argument for all elements in the same row (i.e. setItem () will not move the row).

See also point () and takeItem ().

+12
source

I had the same problem - whenever I wanted to insert new data, I collect the header and setSortingEnabled(true) . I think QTableWidget does not like multiple calls to setSortingEnabled(true) . So I put it in the constructor of my MainWindown, and here it is!

+1
source

I never understood what was wrong with this. I gave up and implemented a custom model / view, which is probably a much better idea.

0
source

This is Karnisov's answer development, also with PyQt. Disabling sorting before doing any setItem() also worked for me, but it made the user's sort selection 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) 

Please note that sorting is still possible with this code. It looks like this view that Qt uses does not move any elements that were already in order, so if we had a table like this:

 NUMBER LETTER OTHER LETTER 1 BC 2 GQ 3 AQ 

If we sort by NUMBER, then nothing changes through ANOTHER LETTER. But if we sort by LETTER, then through another LETTER we get:

 NUMBER LETTER OTHER LETTER 1 BC 3 AQ 2 GQ 

So, if we pre-sorted by LETTER, then ANOTHER LETTER, this code will only make one look using ANOTHER LETTER, and we will finish the second version of the table instead of the first (assuming that the order of the elements were added to the table in NUMBER matches). At this point in my code, all elements are deleted and re-added to update the table, so this can sometimes lead to noticeable changes in the order.

0
source

Source: https://habr.com/ru/post/900471/


All Articles