2

I have tried to get the date from QDateEdit so that I can save it to a database, but when I execute the code in PySide6 it gives me an error:

value = self.ui.dateEdit.date()
var_name = str(value.toPyDate())
    

Error:

PySide6.QtCore.QDate object has no attribute 'toPyDate'

If there is any other method apart from this to get a Python date from QDateEdit and save it to a database I will appreciate it.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Eric paul
  • 101
  • 10

1 Answers1

1

The APIs for converting Qt date/time objects to Python ones differ between PyQt and PySide. For PySide2 and PySide6, you must do:

qdate = QtCore.QDate.currentDate()
pydate = qdate.toPython()

(and this also works the same way for QDateTime objects).

For PyQt5/PyQt6, you must do:

qdate = QtCore.QDate.currentDate()
pydate = qdate.toPyDate()

qdatetime = QtCore.QDateTime.currentDateTime()
pydatetime = qdate.toPyDateTime()

All of these PySide/PyQt APIs return a Python datetime object.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336