Sorting the row based on the value of certain columns of QTableWidget
-
wrote on 7 Feb 2021, 19:51 last edited by
I am having trouble in PyQt5 trying to sort my rows based on a change in value of certain columns in my table. I need to check a column and then if that is higher than the corresponding column in the other rows the rows need to rearrange themselves from highest to lowest.
I am using an array to print my values in the QTableWidget that looks something like this:
self.tableData = [
['FC Inter', 0, 0, 0, 0, 0, 0, 0, 0],
['AC Milan', 0, 0, 0, 0, 0, 0, 0, 0],
['AS Roma', 0, 0, 0, 0, 0, 0, 0, 0]
]
The zeros being variables in my code. I need to check 3 different columns to check what order the rows should be arranged in. Please help -
I am having trouble in PyQt5 trying to sort my rows based on a change in value of certain columns in my table. I need to check a column and then if that is higher than the corresponding column in the other rows the rows need to rearrange themselves from highest to lowest.
I am using an array to print my values in the QTableWidget that looks something like this:
self.tableData = [
['FC Inter', 0, 0, 0, 0, 0, 0, 0, 0],
['AC Milan', 0, 0, 0, 0, 0, 0, 0, 0],
['AS Roma', 0, 0, 0, 0, 0, 0, 0, 0]
]
The zeros being variables in my code. I need to check 3 different columns to check what order the rows should be arranged in. Please helpwrote on 7 Feb 2021, 20:23 last edited byI need to check 3 different columns to check what order the rows should be arranged in
If you stick with a
QTableWidget
, you have to sort your table by your criteria before you put the rows into the table, so go ahead and do so.Or it seems to me if the answer at https://stackoverflow.com/a/18652060/489865 using
You can try to subclass the
QTableWidgetItem
and reimplement operator<()
of it.works that is possible, but I don't know how you override an operator from Python.
If you move to a
QTableView
and have your own model, you can interpose aQSortFilterProxyModel
and overridelessThan()
to do your 3 column comparison.
1/2