Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to sorting date with QString type in QTableWidget?



  • 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


  • Moderators

    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.



  • @Chris-Kawa thank you so much!


Log in to reply