Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    Qt for Python
    2
    2
    467
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      Willkroken 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

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @Willkroken last edited by

        @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.

        1 Reply Last reply Reply Quote 1
        • First post
          Last post