how to set text in qdateedit pyqt5 1

how to set text in qdateedit pyqt5

from PyQt5 import QtCore, QtWidgets


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)

    date_str = "1-Jan-2020"
    # convert str to QDate
    qdate = QtCore.QDate.fromString(date_str, "d-MMM-yyyy")

    widget_date = QtWidgets.QDateEdit()
    # Set the format of how the QDate will be displayed in the widget
    widget_date.setDisplayFormat("d-MMM-yyyy")

    widget_date.setDate(qdate)
    
    widget_date.show()
    
    sys.exit(app.exec_())

Here is what the above code is Doing:
1. We create a QDateEdit widget.
2. We set the display format of the widget to “d-MMM-yyyy”.
3. We convert the string “1-Jan-2020” to a QDate object.
4. We set the QDateEdit widget’s date to the QDate object.
5. We show the widget.

The result is a QDateEdit widget that displays the date “1-Jan-2020”.

Similar Posts