Working on a QGIS plugin, I am struggling to save/load date and time values. Users can enter dates via QDateEdit
widget and times via QTimeEdit
widget. Values are then stored the following:
s = QgsSettings()
# Date
self.Isochrones_Date_setting = self.dlg.Isochrones_Date.date()
s.setValue("otp_plugin/Isochrones_Date", self.Isochrones_Date_setting)
# Time
self.Isochrones_Time_setting = self.dlg.Isochrones_Time.time()
s.setValue("otp_plugin/Isochrones_Time", self.Isochrones_Time_setting)
and shall be read by:
s = QgsSettings()
# Date
self.Isochrones_Date_setting = s.value("otp_plugin/Isochrones_Date", QtCore.QDateTime.currentDateTime())
self.dlg.Isochrones_Date.setDateTime(self.Isochrones_Date_setting)
# Time
self.Isochrones_Time_setting = s.value("otp_plugin/Isochrones_Time", '14:00:00')
self.dlg.Isochrones_Time.setDateTime(self.Isochrones_Time_setting)
However, loading saved values or getting default values fails. With this code I am getting the error
TypeError: setDateTime(self, Union[QDateTime, datetime.datetime]): argument 1 has unexpected type 'QDate'
What is the correct syntax, if not .setDateTime()
? Or do I need to convert QDate to some other date before? If so, how?