简体   繁体   中英

How to convert QDate to Python date object in PySide6

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM