Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PyQT - Make integers and dates in QTableWidget properly sortable
Forum Updated to NodeBB v4.3 + New Features

PyQT - Make integers and dates in QTableWidget properly sortable

Scheduled Pinned Locked Moved Unsolved Language Bindings
2 Posts 2 Posters 1.2k Views 3 Watching
  • 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.
  • N Offline
    N Offline
    NewPyQt
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

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