Home ยป PyQt5 QDateEdit widget

PyQt5 QDateEdit widget

Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer I [1Z0-815] Practice Tests
1 Year Subscription
Oracle Java Certification
Java SE 11 Programmer II [1Z0-816] Practice Tests
Spring Framework Basics Video Course

In this article we will look at the PyQt QDateEdit widget.

The PyQt5 QDateEdit widget is a way for the user to enter in a Date as an input.

Lets look at the methods that are available

Methods

Method Description
date() Returns the current date displayed on the widget,
setDate() Used to set the currently displayed date.
setMinimumDate() Sets the minimum possible date the User can select.
setMaximumDate() Sets the maximum possible date the User can select.

 

Examples

Lets look at some basic examples

This first one we set the date to the current date and set the minimum and maximum available dates to be in 2022, like this

 #set to current date 
dateedit.setDate(QDate.currentDate()) 
#set the minimum date 
dateedit.setMinimumDate(QDate(2022, 1, 1)) 
#set the maximum date 
dateedit.setMaximumDate(QDate(2022, 12, 31))

One interesting thing you may see, try changing the date to a year outside 2022, you will see that this is not allowed because that was what i have set. Probably not very practical but shows how you could restrict to a year range easily.

By setting the minimum and maximum we can restrict what the user enters.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDateEdit, QVBoxLayout
from PyQt5.QtCore import QDate


class MyApp(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        lbl = QLabel('QDateEdit Example')

        dateedit = QDateEdit(self)
        #set to current date
        dateedit.setDate(QDate.currentDate())
        #set the minimum date
        dateedit.setMinimumDate(QDate(2022, 1, 1))
        #set the maximum date
        dateedit.setMaximumDate(QDate(2022, 12, 31))

        vbox = QVBoxLayout()
        vbox.addWidget(lbl)
        vbox.addWidget(dateedit)
        vbox.addStretch()

        self.setLayout(vbox)

        self.setWindowTitle('QDateEdit Example')
        self.setGeometry(300, 300, 300, 200)
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

Running this, will give you the following

Using the setDateRange method

The setDateRange method is the same as using setMinimumDate and setMaximumDate at the same time.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDateEdit, QVBoxLayout
from PyQt5.QtCore import QDate


class MyApp(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        lbl = QLabel('QDateEdit Example')

        dateedit = QDateEdit(self)
        #set to current date
        dateedit.setDate(QDate.currentDate())
        #set the date range
        dateedit.setDateRange(QDate(2022, 1, 1), QDate(2022, 12, 31))
        vbox = QVBoxLayout()
        vbox.addWidget(lbl)
        vbox.addWidget(dateedit)
        vbox.addStretch()

        self.setLayout(vbox)

        self.setWindowTitle('QDateEdit Example')
        self.setGeometry(300, 300, 300, 200)
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

Retrieving values

We can return the values from the QDateEdit

When we pass a date value, we have to use the QDate to convert it into an appropriate format and when we return the date value, we use toPyDate to print it in a more readable format.

It sounds complicated but you can see in the complete code example we have the following showDate() function which is called when the button is pressed

def showDate():
    print(dateedit.date().toPyDate())

Here is the complete example

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QDate
import sys
 
def showDate():
    print(dateedit.date().toPyDate())
 
app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(300, 300, 300, 200)
win.setWindowTitle("QDateEdit Example")
 
dateedit = QtWidgets.QDateEdit(win)
#set to current date
dateedit.setDate(QDate.currentDate())
#set the minimum date
dateedit.setMinimumDate(QDate(2022, 1, 1))
#set the maximum date
dateedit.setMinimumDate(QDate(1970, 1, 1))
dateedit.setMaximumDate(QDate(2022, 12, 31))
dateedit.move(50,50)
 
button = QtWidgets.QPushButton(win)
button.setText("Press me")
button.clicked.connect(showDate)
button.move(50,100)
 
win.show()
sys.exit(app.exec_())

Here is what I saw when I clicked the button after changing the date

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More