Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Sorting the row based on the value of certain columns of QTableWidget
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 2 Posters 1.5k Views
  • 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 Offline
    W Offline
    Willkroken
    wrote on 7 Feb 2021, 19:51 last edited by
    #1

    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

    J 1 Reply Last reply 7 Feb 2021, 20:23
    0
    • W Willkroken
      7 Feb 2021, 19:51

      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

      J Online
      J Online
      JonB
      wrote on 7 Feb 2021, 20:23 last edited by
      #2

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

      1/2

      7 Feb 2021, 19:51

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved