Results 1 to 7 of 7

Thread: QTableView Date display format difference linux vs. Windows

  1. #1
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTableView Date display format difference linux vs. Windows

    I wrote an application that displays a table from a mysql db using QTableView. One of the columns is a Date.

    On Windows it displays as yyyy-mm-dd but on linux is shows as yy-mm-dd.

    Same code, same database.

    The database lives on the Windows machine.

    I have verified that the date format of the database is correct by using MySQL Query Browser running on the Linux machine.

    Did I overlook something?

  2. #2
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView Date display format difference linux vs. Windows

    what you get by running the next code?
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QLocale>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8. QLocale l=QLocale::system();
    9. qDebug()<<l.dateFormat(QLocale::ShortFormat);
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView Date display format difference linux vs. Windows

    on windows: yyyy-mm-dd
    on linux: M/d/yy

    I know how to change the short date format on Windows... if I could only figure out how to do this on linux system wide.


    Added after 1 48 minutes:



    Tried both methods described on the following page:

    http://ccollins.wordpress.com/2009/0...ats-on-ubuntu/

    still the output from the above sample code still shows: "M/d/yy"

    very frustrating...
    Last edited by schnitzel; 18th March 2011 at 18:56.

  4. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView Date display format difference linux vs. Windows

    Ok, just to recap.

    I'm on Ubuntu 10.10 vanilla Qt 4.7.0.
    default locale was en_US with short date M/d/yy which sucks.
    I then changed locale to en_CA and got yy-MM-dd which is better but I'd like 4 digits for year.
    I then changed locale en_CA d_fmt to %Y-%m-%d (and recompiled the locale), yet Qt sample code still gives:
    yy-MM-dd
    I even tried to change d_fmt to %F (confirmed with date +%F) result still:
    yy-MM-dd

    I'm extremely

  5. #5
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView Date display format difference linux vs. Windows

    This is a delegate I use for an application of my own. It is mostly copy-paste from the Qt examples and works fine for me.
    the datedelegate.h:
    Qt Code:
    1. #ifndef DATEDELEGATE_H
    2. #define DATEDELEGATE_H
    3.  
    4. #include <QStyledItemDelegate>
    5.  
    6. class DateDelegate : public QStyledItemDelegate
    7. {
    8. Q_OBJECT
    9. public:
    10. DateDelegate(QObject *parent = 0,const int &dateColumn=0);
    11.  
    12. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    13. const QModelIndex &index) const;
    14.  
    15. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    16. void setModelData(QWidget *editor, QAbstractItemModel *model,
    17. const QModelIndex &index) const;
    18. void updateEditorGeometry(QWidget *editor,
    19. const QStyleOptionViewItem &option, const QModelIndex &index) const;
    20. void paint(QPainter *painter, const QStyleOptionViewItem &option,
    21. const QModelIndex &index) const;
    22.  
    23. signals:
    24.  
    25. public slots:
    26. private:
    27. int dtC;
    28.  
    29. };
    30.  
    31. #endif // DATEDELEGATE_H
    To copy to clipboard, switch view to plain text mode 

    and the datedelegate.cpp
    Qt Code:
    1. #include "datedelegate.h"
    2. #include <QStyleOptionViewItem>
    3. #include <QModelIndex>
    4. #include <QDateEdit>
    5. #include <QPainter>
    6. #include <QApplication>
    7. #include <QTableView>
    8. #include <QDebug>
    9.  
    10. DateDelegate::DateDelegate(QObject *parent, const int &dateColumn) :
    11. QStyledItemDelegate(parent),dtC (dateColumn)
    12. {
    13. }
    14.  
    15. QWidget *DateDelegate::createEditor(QWidget *parent,
    16. const QStyleOptionViewItem &option,
    17. const QModelIndex &index) const
    18. {
    19. if(index.isValid() && index.column() == dtC)
    20. {
    21. QWidget *editor = new QDateEdit(QDate::currentDate(), parent);
    22. return editor;
    23. }
    24. else
    25. return QStyledItemDelegate::createEditor(parent, option, index);
    26. }
    27.  
    28. void DateDelegate::setEditorData(QWidget *editor,
    29. const QModelIndex &index) const
    30. {
    31. if(index.isValid() && index.column() == dtC)
    32. {
    33. QDate value = index.model()->data(index, Qt::EditRole).toDate();
    34.  
    35. QDateEdit *dateEdit = static_cast<QDateEdit*>(editor);
    36. dateEdit->setCalendarPopup(true);
    37. dateEdit->setDate(value);
    38. }
    39. else
    40. QStyledItemDelegate::setEditorData(editor,index);
    41. }
    42.  
    43. void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    44. const QModelIndex &index) const
    45. {
    46. if(index.isValid() && index.column() == dtC)
    47. {
    48. QDateEdit *dateEdit = static_cast<QDateEdit*>(editor);
    49. QDate value = dateEdit->date();
    50.  
    51. model->setData(index, value.toString("yyyy-MM-dd"), Qt::EditRole);
    52. }
    53.  
    54. else
    55. {
    56. QStyledItemDelegate::setModelData(editor,model,index);
    57. }
    58. }
    59.  
    60. void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
    61. const QModelIndex &index) const
    62. {
    63. if(index.isValid() && index.column() == dtC)
    64. editor->setGeometry(option.rect);
    65. else
    66. QStyledItemDelegate::updateEditorGeometry(editor, option, index);
    67.  
    68. }
    69.  
    70. void DateDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    71. const QModelIndex &index) const
    72. {
    73. painter->save();
    74. if(index.isValid() && index.column() == dtC)
    75. {
    76. QDate value = index.model()->data(index, Qt::EditRole).toDate();
    77. if (option.state & QStyle::State_Active)
    78. {
    79. QApplication::style()->drawItemText(painter, option.rect, Qt::AlignHCenter|Qt::AlignVCenter,
    80. option.palette, true, value.toString("dd/MM/yyyy"),
    81. (option.state&QStyle::State_Selected)?QPalette::HighlightedText:QPalette::WindowText);
    82. }
    83. else
    84. {
    85. QApplication::style()->drawItemText(painter, option.rect, Qt::AlignHCenter|Qt::AlignVCenter,
    86. option.palette, true, value.toString("dd/MM/yyyy"),
    87. QPalette::WindowText);
    88. }
    89. }
    90. else
    91. {
    92. QStyledItemDelegate::paint(painter, option, index);
    93. }
    94. painter->restore();
    95. }
    To copy to clipboard, switch view to plain text mode 

    The writing to model is done in line 51 in ISO format for a SQLite database.
    Change lines 80 and 86 to whatever format you like for the tableView.

    And for the sake of completeness:
    Qt Code:
    1. #include "datedelegate.h"
    2. ......
    3. DateDelegate dateDelegate=new DateDelegate(this,column);
    4. ui->tableView->setItemDelegate(dateDelegate);
    5. ......
    To copy to clipboard, switch view to plain text mode 

    It is possible to rewrite the delegate without the column variable and use the setItemDelegateForColumn method of the tableView.


    Added after 14 minutes:


    Having just read the QLocale source code I think the LC_TIME is never read and I would not mess with the LANG or LC_ALL in my working machine. So just for the fun of it I wrote this trying to override what Qt reads from system
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QLocale>
    3. #include <QDebug>
    4. #include <QVariant>
    5. #include <QDate>
    6.  
    7. class MyLocal : QSystemLocale
    8. {
    9. #include <QSystemLocale>
    10. public:
    11. MyLocal() { };
    12.  
    13. QVariant query(QueryType type, QVariant in) const
    14. {
    15. if (type==QSystemLocale::DateToStringShort)
    16. {
    17. QDate inDate=in.toDate();
    18. return inDate.toString("yy-M-d");
    19. }
    20. else if (type==QSystemLocale::DateToStringLong)
    21. {
    22. QDate inDate=in.toDate();
    23. return inDate.toString("yyyy-MM-dd");
    24. }
    25. return QSystemLocale::query(type, in);
    26. }
    27. };
    28.  
    29. int main(int argc, char *argv[])
    30. {
    31. QCoreApplication a(argc, argv);
    32. MyLocal m;
    33. qDebug()<<QDate::currentDate().toString(Qt::DefaultLocaleShortDate);
    34. qDebug()<<QDate::currentDate().toString(Qt::DefaultLocaleLongDate);
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

    which is pretty stupid because the same could be done pretty simple as
    QDate::currentDate().toString("yy-MM-dd")
    Last edited by Rhayader; 18th March 2011 at 20:39.

  6. The following user says thank you to Rhayader for this useful post:

    schnitzel (18th March 2011)

  7. #6
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView Date display format difference linux vs. Windows

    Hi Rhayader,
    thanks for investigating. I'll play around with this tonight.

  8. #7
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView Date display format difference linux vs. Windows

    overriding QSystemLocale worked like a charm.

Similar Threads

  1. Date and Time Difference
    By MIH1406 in forum Newbie
    Replies: 4
    Last Post: 20th August 2012, 15:03
  2. Date format is not display
    By sosanjay in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2009, 10:26
  3. Replies: 1
    Last Post: 15th April 2009, 09:00
  4. Date and Time format.
    By kaushal_gaurav in forum Qt Programming
    Replies: 3
    Last Post: 12th August 2008, 11:36
  5. Replies: 4
    Last Post: 9th January 2008, 15:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.