Search code examples
pythondatepyqtlocaleqdatetime

QDateEdit.date().toString("MMMM dd, yyyy") does not display months in English


I have a QDateEdit which is called date. No matter what I do, I'm not able to make it display the months in English, it always displays them in my current locale.

I've already tried:

self.ui.date.setLocale(QtCore.QLocale(
    QtCore.QLocale.English, QtCore.QLocale.UnitedStates))

self.ui.date.calendarWidget().setLocale(QtCore.QLocale(
    QtCore.QLocale.English, QtCore.QLocale.UnitedStates))

By doing this, the calendarWidget that pops-up when I click the widget changed to English. However, if I do:

print ui.date.date().toString("MMMM dd, yyyy")

I still get the months in Portuguese instead of English. I also tried to change the locale with python's locale module but it didn't yield any results.


Solution

  • Use QLocale.toString method

    e.g:

    print(QLocale(QLocale.English, QLocale.UnitedStates).toString(self.ui.date.date(), "MMMM dd, yyyy"))`
    

    QDate is a locale independent representation of the date. You need to specify the locale when formatting ...