Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to sorting date with QString type in QTableWidget?
Forum Updated to NodeBB v4.3 + New Features

How to sorting date with QString type in QTableWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.6k Views 1 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.
  • V Offline
    V Offline
    vinh
    wrote on last edited by vinh
    #1

    Default item in QTableWidget is QString, when I read date with format dd/MM/yyyy (13/12/2020), sort date column same only sort with first character.
    This is date column new load:
    9aab49a0-9f50-48c9-aad0-754a37531d92-image.png
    This is table date column after sort:
    1c0673e8-73ec-4c1a-bf5d-8d7b0866ce6d-image.png
    sorting is incorrect!
    How to sort date by QString type?
    *format I use dd/MM/yyyy

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      If you can just store the date as date, not string and it will sort correctly, so instead of

      QTableWidgetItem* item = new QTableWidgetItem("12/11/2020");
      

      do

      QTableWidgetItem* item = new QTableWidgetItem();
      item->setData(Qt::DisplayRole, QDate(2020,11,12));
      

      If you really want strings you can switch to QTableView and implement custom sorting with QSortFilterProxyModel.

      V 1 Reply Last reply
      3
      • Chris KawaC Chris Kawa

        If you can just store the date as date, not string and it will sort correctly, so instead of

        QTableWidgetItem* item = new QTableWidgetItem("12/11/2020");
        

        do

        QTableWidgetItem* item = new QTableWidgetItem();
        item->setData(Qt::DisplayRole, QDate(2020,11,12));
        

        If you really want strings you can switch to QTableView and implement custom sorting with QSortFilterProxyModel.

        V Offline
        V Offline
        vinh
        wrote on last edited by
        #3

        @Chris-Kawa thank you so much!

        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