How to convert QDate to Python date object in PySide6

2.2k Views Asked by At

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.

1

There are 1 best solutions below

0
ekhumoro On

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.