PyQt - QDateEdit



A QDateEdit is a widget that allows users to input and edit dates. Think of it as a user where we have date picker to choose a specific date.

Imagine we are filling out a form, and there is a field where we need to enter the birthdate. The QDateEdit provides an intuitive way to do that. We can type the date directly using the keyboard or use the arrow keys to increment or decrement the date. It is like adjusting a calendar manually.

Following are some key properties of QDateEdit −

  • Date − This property holds the date displayed by the widget. Its the actual date youve selected.
  • Minimum Date − We can set the earliest date that users are allowed to choose. For example, if we are booking an appointment, we might want to prevent users from selecting dates in the past.
  • Maximum Date − Similarly, We can set the latest date users can pick. May be we are creating a future event scheduler, and we dont want dates beyond a certain point.
  • Display Format − We can customize how the date appears in the widget. It could be something like "dd/MM/yyyy".

Properties of QDateEdit

There are various properties and methods by QDateEdit are implemented in QDateTimeEdit. Below the relatable properties that can be used for this class.

  • The date holds the date displayed by the widget.
  • minimumDate − User can set the earliest date.
  • maximumDate − User can set the new or current date.
  • displayFormat − This method defines the string formats and it displays in the widget.

Example 1

We use the various methods of QDateEdit to display the date edits.

import sys
from PyQt6.QtWidgets import *
from PyQt6 import QtCore, QtGui
from PyQt6.QtGui import *
from PyQt6.QtCore import *
class Window(QMainWindow):

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

      # setting title
      self.setWindowTitle("Date Edit(PyQt)")

      # setting geometry
      self.setGeometry(100, 100, 500, 400)

      # calling method
      self.UiComponents()

      # showing all the widgets
      self.show()

   # method for components
   def UiComponents(self):

      # creating a QDateEdit widget
      date = QDateEdit(self)

      # setting geometry of the date edit
      date.setGeometry(100, 100, 150, 40)

      # creating a label
      label = QLabel("Tutorialspoint", self)

      # setting geometry
      label.setGeometry(100, 150, 200, 60)

      # making label multiline
      label.setWordWrap(True)

      # adding action to the date when enter key is pressed
      date.editingFinished.connect(lambda: date_method())

         # method called by date edit
         def date_method():

            # getting the date
            value = date.date()

            # setting text to the label
            label.setText("Selected Date : " + str(value))

# create pyqt5 app
App = QApplication(sys.argv)

# create the instance of our Window
window = Window()

# start the app
sys.exit(App.exec())

Output

The above code produces the following output −

qdateedit_ex_one

Example 2

Here, we will display the date edits using QDateEdit.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QDateEdit
from PyQt6.QtCore import QDate

app = QApplication(sys.argv)
wid = QMainWindow()
wid.setGeometry(400, 400, 250, 250)
wid.setWindowTitle("PyQt")

date = QDateEdit(wid)
date.setMinimumDate(QDate(1900, 1, 1))
date.setMaximumDate(QDate(2400, 12, 31))
date.move(50, 50)

wid.show()
sys.exit(app.exec())

Output

The above code produces the following output −

qdateedit_ex_two
Advertisements