class QCalendarWidget

The QCalendarWidget class provides a monthly based calendar widget allowing the user to select a date. More

Inheritance diagram of PySide6.QtWidgets.QCalendarWidget

Synopsis

Properties

Methods

Virtual methods

Slots

Signals

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

../../_images/fusion-calendarwidget.png

The widget is initialized with the current month and year, but QCalendarWidget provides several public slots to change the year and month that is shown.

By default, today’s date is selected, and the user can select a date using both mouse and keyboard. The currently selected date can be retrieved using the selectedDate() function. It is possible to constrain the user selection to a given date range by setting the minimumDate and maximumDate properties. Alternatively, both properties can be set in one go using the setDateRange() convenience slot. Set the selectionMode property to NoSelection to prohibit the user from selecting at all. Note that a date also can be selected programmatically using the setSelectedDate() slot.

The currently displayed month and year can be retrieved using the monthShown() and yearShown() functions, respectively.

A newly created calendar widget uses abbreviated day names, and both Saturdays and Sundays are marked in red. The calendar grid is not visible. The week numbers are displayed, and the first column day is the first day of the week for the calendar’s locale.

The notation of the days can be altered to a single letter abbreviations (“M” for “Monday”) by setting the horizontalHeaderFormat property to SingleLetterDayNames . Setting the same property to LongDayNames makes the header display the complete day names. The week numbers can be removed by setting the verticalHeaderFormat property to NoVerticalHeader . The calendar grid can be turned on by setting the gridVisible property to true using the setGridVisible() function:

../../_images/qcalendarwidget-grid.png
calendar = QCalendarWidget()
calendar.setGridVisible(True)

Finally, the day in the first column can be altered using the setFirstDayOfWeek() function.

The QCalendarWidget class also provides three signals, selectionChanged() , activated() and currentPageChanged() making it possible to respond to user interaction.

The rendering of the headers, weekdays or single days can be largely customized by setting QTextCharFormat’s for some special weekday, a special date or for the rendering of the headers.

Only a subset of the properties in QTextCharFormat are used by the calendar widget. Currently, the foreground, background and font properties are used to determine the rendering of individual cells in the widget.

class HorizontalHeaderFormat

This enum type defines the various formats the horizontal header can display.

Constant

Description

QCalendarWidget.SingleLetterDayNames

The header displays a single letter abbreviation for day names (e.g. M for Monday).

QCalendarWidget.ShortDayNames

The header displays a short abbreviation for day names (e.g. Mon for Monday).

QCalendarWidget.LongDayNames

The header displays complete day names (e.g. Monday).

QCalendarWidget.NoHorizontalHeader

The header is hidden.

class VerticalHeaderFormat

This enum type defines the various formats the vertical header can display.

Constant

Description

QCalendarWidget.ISOWeekNumbers

The header displays ISO week numbers as described by QDate::weekNumber().

QCalendarWidget.NoVerticalHeader

The header is hidden.

class SelectionMode

This enum describes the types of selection offered to the user for selecting dates in the calendar.

Constant

Description

QCalendarWidget.NoSelection

Dates cannot be selected.

QCalendarWidget.SingleSelection

Single dates can be selected.

See also

selectionMode

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property dateEditAcceptDelayᅟ: int

This property holds the time an inactive date edit is shown before its contents are accepted.

If the calendar widget’s date edit is enabled , this property specifies the amount of time (in milliseconds) that the date edit remains open after the most recent user input. Once this time has elapsed, the date specified in the date edit is accepted and the popup is closed.

By default, the delay is defined to be 1500 milliseconds (1.5 seconds).

Access functions:
property dateEditEnabledᅟ: bool

This property holds whether the date edit popup is enabled.

If this property is enabled, pressing a non-modifier key will cause a date edit to popup if the calendar widget has focus, allowing the user to specify a date in the form specified by the current locale.

By default, this property is enabled.

The date edit is simpler in appearance than QDateEdit , but allows the user to navigate between fields using the left and right cursor keys, increment and decrement individual fields using the up and down cursor keys, and enter values directly using the number keys.

Access functions:
property firstDayOfWeekᅟ: Qt.DayOfWeek

This property holds a value identifying the day displayed in the first column..

By default, the day displayed in the first column is the first day of the week for the calendar’s locale.

Access functions:
property gridVisibleᅟ: bool

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

This property holds whether the table grid is displayed..

qcalendarwidget-grid1

calendar = QCalendarWidget()
calendar.setGridVisible(True)

The default value is false.

Access functions:
property horizontalHeaderFormatᅟ: QCalendarWidget.HorizontalHeaderFormat

This property holds the format of the horizontal header..

The default value is ShortDayNames .

Access functions:
property maximumDateᅟ: QDate

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

This property holds the maximum date of the currently specified date range..

The user will not be able to select a date which is after the currently set maximum date.

../../_images/qcalendarwidget-maximum.png
calendar = QCalendarWidget()
calendar.setGridVisible(True)
calendar.setMaximumDate(QDate(2006, 7, 3))

When setting a maximum date, the minimumDate and selectedDate properties are adjusted if the selection range becomes invalid. If the provided date is not a valid QDate object, the setMaximumDate() function does nothing.

The default maximum date is December 31, 9999 CE. You can restore this default by calling clearMaximumDate() (since Qt 6.6).

See also

setDateRange()

Access functions:
property minimumDateᅟ: QDate

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

This property holds the minimum date of the currently specified date range..

The user will not be able to select a date that is before the currently set minimum date.

../../_images/qcalendarwidget-minimum.png
calendar = QCalendarWidget()
calendar.setGridVisible(True)
calendar.setMinimumDate(QDate(2006, 6, 19))

When setting a minimum date, the maximumDate and selectedDate properties are adjusted if the selection range becomes invalid. If the provided date is not a valid QDate object, the setMinimumDate() function does nothing.

The default minimum date is November 25, 4714 BCE. You can restore this default by calling clearMinimumDate() (since Qt 6.6).

See also

setDateRange()

Access functions:
property navigationBarVisibleᅟ: bool

This property holds whether the navigation bar is shown or not.

When this property is true (the default), the next month, previous month, month selection, year selection controls are shown on top.

When the property is set to false, these controls are hidden.

Access functions:
property selectedDateᅟ: QDate

This property holds the currently selected date..

The selected date must be within the date range specified by the minimumDate and maximumDate properties. By default, the selected date is the current date.

See also

setDateRange()

Access functions:
property selectionModeᅟ: QCalendarWidget.SelectionMode

This property holds the type of selection the user can make in the calendar.

When this property is set to SingleSelection , the user can select a date within the minimum and maximum allowed dates, using either the mouse or the keyboard.

When the property is set to NoSelection , the user will be unable to select dates, but they can still be selected programmatically. Note that the date that is selected when the property is set to NoSelection will still be the selected date of the calendar.

The default value is SingleSelection .

Access functions:
property verticalHeaderFormatᅟ: QCalendarWidget.VerticalHeaderFormat

This property holds the format of the vertical header..

The default value is QCalendarWidget::ISOWeekNumber.

Access functions:
__init__([parent=None])
Parameters:

parentQWidget

Constructs a calendar widget with the given parent.

The widget is initialized with the current month and year, and the currently selected date is today.

See also

setCurrentPage()

activated(date)
Parameters:

dateQDate

This signal is emitted whenever the user presses the Return or Enter key or double-clicks a date in the calendar widget.

calendar()
Return type:

QCalendar

Report the calendar system in use by this widget.

See also

setCalendar()

clearMaximumDate()

Reset function of property maximumDateᅟ .

clearMinimumDate()

Reset function of property minimumDateᅟ .

clicked(date)
Parameters:

dateQDate

This signal is emitted when a mouse button is clicked. The date the mouse was clicked on is specified by date. The signal is only emitted when clicked on a valid date, e.g., dates are not outside the minimumDate() and maximumDate() . If the selection mode is NoSelection , this signal will not be emitted.

currentPageChanged(year, month)
Parameters:
  • year – int

  • month – int

This signal is emitted when the currently shown month is changed. The new year and month are passed as parameters.

See also

setCurrentPage()

dateEditAcceptDelay()
Return type:

int

Getter of property dateEditAcceptDelayᅟ .

dateTextFormat()
Return type:

Dictionary with keys of type .QDate and values of type QTextCharFormat.

Returns a QMap from QDate to QTextCharFormat showing all dates that use a special format that alters their rendering.

dateTextFormat(date)
Parameters:

dateQDate

Return type:

QTextCharFormat

Returns a QTextCharFormat for date. The char format can be empty if the date is not renderd specially.

firstDayOfWeek()
Return type:

DayOfWeek

Getter of property firstDayOfWeekᅟ .

headerTextFormat()
Return type:

QTextCharFormat

Returns the text char format for rendering the header.

horizontalHeaderFormat()
Return type:

HorizontalHeaderFormat

Getter of property horizontalHeaderFormatᅟ .

isDateEditEnabled()
Return type:

bool

Getter of property dateEditEnabledᅟ .

isGridVisible()
Return type:

bool

Getter of property gridVisibleᅟ .

isNavigationBarVisible()
Return type:

bool

Getter of property navigationBarVisibleᅟ .

maximumDate()
Return type:

QDate

See also

setMaximumDate()

Getter of property maximumDateᅟ .

minimumDate()
Return type:

QDate

See also

setMinimumDate()

Getter of property minimumDateᅟ .

monthShown()
Return type:

int

Returns the currently displayed month. Months are numbered from 1 to 12.

paintCell(painter, rect, date)
Parameters:

Paints the cell specified by the given date, using the given painter and rect.

selectedDate()
Return type:

QDate

Getter of property selectedDateᅟ .

selectionChanged()

This signal is emitted when the currently selected date is changed.

The currently selected date can be changed by the user using the mouse or keyboard, or by the programmer using setSelectedDate() .

See also

selectedDate()

selectionMode()
Return type:

SelectionMode

Getter of property selectionModeᅟ .

setCalendar(calendar)
Parameters:

calendarQCalendar

Set c as the calendar system to be used by this widget.

The widget can use any supported calendar system. By default, it uses the Gregorian calendar.

See also

calendar()

setCurrentPage(year, month)
Parameters:
  • year – int

  • month – int

Displays the given month of the given year without changing the selected date. Use the setSelectedDate() function to alter the selected date.

The currently displayed month and year can be retrieved using the monthShown() and yearShown() functions respectively.

setDateEditAcceptDelay(delay)
Parameters:

delay – int

Setter of property dateEditAcceptDelayᅟ .

setDateEditEnabled(enable)
Parameters:

enable – bool

Setter of property dateEditEnabledᅟ .

setDateRange(min, max)
Parameters:

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Defines a date range by setting the minimumDate and maximumDate properties.

The date range restricts the user selection, i.e. the user can only select dates within the specified date range. Note that

calendar = QCalendarWidget()
calendar.setDateRange(min, max)

is analogous to

calendar = QCalendarWidget()
calendar.setMinimumDate(min)
calendar.setMaximumDate(max)

If either the min or max parameters are not valid QDate objects, this function does nothing.

setDateTextFormat(date, format)
Parameters:

Sets the format used to render the given date to that specified by format.

If date is null, all date formats are cleared.

See also

dateTextFormat()

setFirstDayOfWeek(dayOfWeek)
Parameters:

dayOfWeekDayOfWeek

See also

firstDayOfWeek()

Setter of property firstDayOfWeekᅟ .

setGridVisible(show)
Parameters:

show – bool

See also

isGridVisible()

Setter of property gridVisibleᅟ .

setHeaderTextFormat(format)
Parameters:

formatQTextCharFormat

Sets the text char format for rendering the header to format. If you also set a weekday text format, this format’s foreground and background color will take precedence over the header’s format. The other formatting information will still be decided by the header’s format.

setHorizontalHeaderFormat(format)
Parameters:

formatHorizontalHeaderFormat

Setter of property horizontalHeaderFormatᅟ .

setMaximumDate(date)
Parameters:

dateQDate

See also

maximumDate()

Setter of property maximumDateᅟ .

setMinimumDate(date)
Parameters:

dateQDate

See also

minimumDate()

Setter of property minimumDateᅟ .

setNavigationBarVisible(visible)
Parameters:

visible – bool

Setter of property navigationBarVisibleᅟ .

setSelectedDate(date)
Parameters:

dateQDate

See also

selectedDate()

Setter of property selectedDateᅟ .

setSelectionMode(mode)
Parameters:

modeSelectionMode

See also

selectionMode()

Setter of property selectionModeᅟ .

setVerticalHeaderFormat(format)
Parameters:

formatVerticalHeaderFormat

Setter of property verticalHeaderFormatᅟ .

setWeekdayTextFormat(dayOfWeek, format)
Parameters:

Sets the text char format for rendering of day in the week dayOfWeek to format. The format will take precedence over the header format in case of foreground and background color. Other text formatting information is taken from the headers format.

showNextMonth()

Shows the next month relative to the currently displayed month. Note that the selected date is not changed.

showNextYear()

Shows the currently displayed month in the next year relative to the currently displayed year. Note that the selected date is not changed.

showPreviousMonth()

Shows the previous month relative to the currently displayed month. Note that the selected date is not changed.

showPreviousYear()

Shows the currently displayed month in the previous year relative to the currently displayed year. Note that the selected date is not changed.

showSelectedDate()

Shows the month of the selected date.

showToday()

Shows the month of the today’s date.

updateCell(date)
Parameters:

dateQDate

Updates the cell specified by the given date unless updates are disabled or the cell is hidden.

updateCells()

Updates all visible cells unless updates are disabled.

See also

updateCell()

verticalHeaderFormat()
Return type:

VerticalHeaderFormat

Getter of property verticalHeaderFormatᅟ .

weekdayTextFormat(dayOfWeek)
Parameters:

dayOfWeekDayOfWeek

Return type:

QTextCharFormat

Returns the text char format for rendering of day in the week dayOfWeek.

yearShown()
Return type:

int

Returns the year of the currently displayed month. Months are numbered from 1 to 12.