johngull johngull - 1 year ago 150
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
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
but i can't find it.

Answer Source

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() );;

It look like this in Mac enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download