Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sorting the row based on the value of certain columns of QTableWidget



  • 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



  • @Willkroken

    I 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 a QSortFilterProxyModel and override lessThan() to do your 3 column comparison.


Log in to reply