I have a Pyside6 application which contains a QTableWidget with 10 columns and approx. 4000 rows. When I click on a column name, I want to sort the table by the values of the clicked column. All columns except the first one contain numbers with two decimal places (I know that the cells display them as text).
When trying to sort the values I encounter the following problems:
if the numbers inside the column which is used to sort have a different lengths (e.G. 100.00, 3500.00, 10.00), the sorting is just correct inside the different lengths but it does not sort it from 0.00 to the biggest number as expected.
The sorting itself is pretty slow when I have a few thousand rows in the table. During the sorting process, the app freezes and it not usable until the sorting is completed. This could take up to 10 seconds.
This is the code how I populate my data to the table:
#Set results df as row in table widget
# Get the current row count
current_row_count = self.table_widget.rowCount()
# Insert a new row at the end
self.table_widget.insertRow(current_row_count)
column_count = 0
#for result in results_list:
for column in results_df:
current_value = results_df[column].values[0]
#print("Current value: ", current_value)
if not column_count == 0:
table_item = QTableWidgetItem(f"{current_value:.2f}")
else:
table_item = QTableWidgetItem(current_value)
table_item.setFlags(table_item.flags() & ~Qt.ItemIsEditable)
# Set alignment to center
table_item.setTextAlignment(Qt.AlignCenter)
self.table_widget.setItem(current_row_count, column_count, table_item)
column_count += 1
This is my code to sort the table by clicking on a column header:
self.table_widget.setSortingEnabled(True)
self.table_widget.horizontalHeader().sectionClicked.connect(self.sort_table)
def sort_table(self, column):
current_order = self.table_widget.horizontalHeader().sortIndicatorOrder()
if current_order == Qt.AscendingOrder:
new_order = Qt.DescendingOrder
else:
new_order = Qt.AscendingOrder
self.table_widget.sortItems(column, new_order)
To solve the first problem (sorting not correct) I tried to populate my table in a different way by using this code (using setData) but I just get empty cells and I don't know why:
#Set results df as row in table widget
# Get the current row count
current_row_count = self.table_widget.rowCount()
# Insert a new row at the end
self.table_widget.insertRow(current_row_count)
column_count = 0
#for result in results_list:
for column in results_df:
current_value = results_df[column].values[0]
#print("Current value: ", current_value)
if not column_count == 0:
current_value_rounded = round(current_value, 2)
table_item = QTableWidgetItem()
table_item.setData(Qt.DisplayRole, current_value_rounded)
else:
table_item = QTableWidgetItem(current_value)
table_item.setFlags(table_item.flags() & ~Qt.ItemIsEditable)
# Set alignment to center
table_item.setTextAlignment(Qt.AlignCenter)
self.table_widget.setItem(current_row_count, column_count, table_item)
column_count += 1
To the first problem: Does someone know why this second code example is not working with setData to populate my Table? To the second problem: Does someone has an idea how to speed up the sorting process when it is working correctly?