4

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.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Eduardo
  • 631
  • 1
  • 12
  • 25

1 Answers1

4

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 ...

Aleš Erjavec
  • 1,086
  • 7
  • 11
  • The problem is that I do not want to get the current time. Instead, I'm trying to read it from a QDataEdit widget, so that the user can insert the data by himself – Eduardo Aug 14 '17 at 17:49
  • 1
    @Eduardo. Why are you trying to manipulate *locale-dependant* date strings? What are you actually trying to do? – ekhumoro Aug 14 '17 at 20:47
  • @ekhumoro it is a software to record the manufacture process of a laser production line. So basically it's a GUI that when the user is going to perform some process, let say attach a lens on the laser, he has to add measurements and information regarding the process into the software, e.g. date, time of start, time of end, optical power, etc.. So he inserts the date in a QDateEdit. The problem is that the report is made in english and computers here have different locale in their OS' settings. I would like it to be compliant with any computer settings – Eduardo Aug 15 '17 at 11:31
  • 2
    @Eduardo. This answer seems to do exactly what you're asking for. Have you actually tried it? And if so, how did the results differ from what you were expecting? – ekhumoro Aug 15 '17 at 19:23
  • @ekhumoro It indeed worked. The first time when I looked at it I misread. I thought it was using Locale to get the current time instead of the widget time. Thank you both – Eduardo Aug 15 '17 at 19:38