johngull johngull - 1 month ago 17
C++ Question

How to use Qt::SystemLocaleShortDate in QDateTimeEdit?

In QDateTimeEdit it is possible to set format by string with the

setDisplayFormat(const QString &format)
, but i see no function which receive
Qt::DateFormat
enumeration instead of string.

My goal is to have format of QDateEdit depending on user locale.
Maybe it is possible to fetch string format which is used for Qt::SystemLocaleShortDate in
fromString
and
toString
but i can't find it.

Answer

You can use this code to set display format.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QDateTimeEdit w;
    QLocale currentLocale = QLocale::system();
    w.setDisplayFormat( currentLocale.dateFormat( QLocale::ShortFormat ) + " " + currentLocale.timeFormat( QLocale::ShortFormat ) );
    w.setDateTime( QDateTime::currentDateTime() );
    w.show();
    a.exec();
}

It look like this in Mac enter image description here

Comments