PyQT - Make integers and dates in QTableWidget properly sortable



  • Hi.
    I put the data into my QTableWidget tableView using a loop:

    for i in range(0, len(res)):
                    self.tableView.setItem(i, 2, QTableWidgetItem(str(create_dataframe(res)[2][i])))
                    self.tableView.setItem(i, 3, QTableWidgetItem(str(create_dataframe(res)[3][i])))
    

    where

    create_dataframe(res)[2][i]
    

    returns value of class 'int' and

    create_dataframe(res)[3][i]
    

    returns value of class 'datetime.datetime' (like '2017-03-25 16:51:24'). The question is: how do I make this items properly sortable through

    self.tableView.setSortingEnabled(True)
    

    , i.e. not as strings, but as integers and datetime respectively? I know that I should use setData and Qt.DisplayRole, but could you please give an example using this short piece of code?
    Thank you.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you mean that you want to sort your QTableWidget by the date string ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.