PyQt: How to convert from Python to QDate style

pyqt, python

Solution

The `fromString` method of `QDate` will do the conversion for you:

myPythonicDate='2014-04-17'
qtDate = QtCore.QDate.fromString(myPythonicDate, 'yyyy-MM-dd')
print qtDate.year(), qtDate.month(), qtDate.day()
2014 4 17

`yyyy-MM-dd` is the format of your date; `yyyy` indicates a four digit year while `MM` and `dd` indicate month/date are two characters. See the `fromString` documentation for a list of additional field options.

Problem

QtCore.QDate accepts three integers as arguments: year first, then month, then date. Example: ``` myDateEdit.setMaximumDate(QtCore.QDate(2015, 12, 28)) ``` In order to "convert" QDate 'style' to Python use: ``` myPythonicDate=myDateEdit.date().toPyDate() print myPythonicDate '2014-04-17' ``` The question is: ``` # With a variable: myPythonicDate='2014-04-17' ``` what syntax is used to declare QDate object... the following won't work: ``` myDateEdit.setDate(QtCore.QDate(myPythonicDate)) ```

Original source