QCalendarWidget Class

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

Header: #include <QCalendarWidget>
qmake: QT += widgets
Since: Qt 4.2
Inherits: QWidget

This class was introduced in Qt 4.2.

Public Types

enum HorizontalHeaderFormat { SingleLetterDayNames, ShortDayNames, LongDayNames, NoHorizontalHeader }
enum SelectionMode { NoSelection, SingleSelection }
enum VerticalHeaderFormat { ISOWeekNumbers, NoVerticalHeader }

Properties

Public Functions

int dateEditAcceptDelay() const
Qt::DayOfWeek firstDayOfWeek() const
QCalendarWidget::HorizontalHeaderFormat horizontalHeaderFormat() const
bool isDateEditEnabled() const
bool isGridVisible() const
bool isNavigationBarVisible() const
QDate maximumDate() const
QDate minimumDate() const
QDate selectedDate() const
QCalendarWidget::SelectionMode selectionMode() const
void setDateEditAcceptDelay(int delay)
void setDateEditEnabled(bool enable)
void setFirstDayOfWeek(Qt::DayOfWeek dayOfWeek)
void setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat format)
void setMaximumDate(const QDate &date)
void setMinimumDate(const QDate &date)
void setSelectionMode(QCalendarWidget::SelectionMode mode)
void setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat format)
QCalendarWidget::VerticalHeaderFormat verticalHeaderFormat() const

Public Slots

void setGridVisible(bool show)
void setNavigationBarVisible(bool visible)
void setSelectedDate(const QDate &date)

Detailed Description

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 QCalendarWidget::SingleLetterDayNames. Setting the same property to QCalendarWidget::LongDayNames makes the header display the complete day names. The week numbers can be removed by setting the verticalHeaderFormat property to QCalendarWidget::NoVerticalHeader. The calendar grid can be turned on by setting the gridVisible property to true using the setGridVisible() function:

 QCalendarWidget *calendar;
 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.

See also QDate, QDateEdit, and QTextCharFormat.

Member Type Documentation

enum QCalendarWidget::HorizontalHeaderFormat

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

ConstantValueDescription
QCalendarWidget::SingleLetterDayNames1The header displays a single letter abbreviation for day names (e.g. M for Monday).
QCalendarWidget::ShortDayNames2The header displays a short abbreviation for day names (e.g. Mon for Monday).
QCalendarWidget::LongDayNames3The header displays complete day names (e.g. Monday).
QCalendarWidget::NoHorizontalHeader0The header is hidden.

See also horizontalHeaderFormat() and VerticalHeaderFormat.

enum QCalendarWidget::SelectionMode

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

ConstantValueDescription
QCalendarWidget::NoSelection0Dates cannot be selected.
QCalendarWidget::SingleSelection1Single dates can be selected.

See also selectionMode.

enum QCalendarWidget::VerticalHeaderFormat

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

ConstantValueDescription
QCalendarWidget::ISOWeekNumbers1The header displays ISO week numbers as described by QDate::weekNumber().
QCalendarWidget::NoVerticalHeader0The header is hidden.

See also verticalHeaderFormat() and HorizontalHeaderFormat.

Property Documentation

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).

This property was introduced in Qt 4.3.

Access functions:

int dateEditAcceptDelay() const
void setDateEditAcceptDelay(int delay)

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.

This property was introduced in Qt 4.3.

Access functions:

bool isDateEditEnabled() const
void setDateEditEnabled(bool enable)

See also QCalendarWidget::dateEditAcceptDelay.

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:

Qt::DayOfWeek firstDayOfWeek() const
void setFirstDayOfWeek(Qt::DayOfWeek dayOfWeek)

gridVisible : bool

This property holds whether the table grid is displayed.

 QCalendarWidget *calendar;
 calendar->setGridVisible(true);

The default value is false.

Access functions:

bool isGridVisible() const
void setGridVisible(bool show)

horizontalHeaderFormat : HorizontalHeaderFormat

This property holds the format of the horizontal header.

The default value is QCalendarWidget::ShortDayNames.

Access functions:

QCalendarWidget::HorizontalHeaderFormat horizontalHeaderFormat() const
void setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat format)

maximumDate : QDate

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.

 QCalendarWidget *calendar;
 calendar->setGridVisible(true);
 calendar->setMaximumDate(QDate(2006, 7, 3));

By default, the maximum date is the last day the QDate class can handle.

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.

Access functions:

QDate maximumDate() const
void setMaximumDate(const QDate &date)

See also setDateRange().

minimumDate : QDate

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.

 QCalendarWidget *calendar;
 calendar->setGridVisible(true);
 calendar->setMinimumDate(QDate(2006, 6, 19));

By default, the minimum date is the earliest date that the QDate class can handle.

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.

Access functions:

QDate minimumDate() const
void setMinimumDate(const QDate &date)

See also setDateRange().

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.

This property was introduced in Qt 4.3.

Access functions:

bool isNavigationBarVisible() const
void setNavigationBarVisible(bool visible)

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.

Access functions:

QDate selectedDate() const
void setSelectedDate(const QDate &date)

See also setDateRange().

selectionMode : 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:

QCalendarWidget::SelectionMode selectionMode() const
void setSelectionMode(QCalendarWidget::SelectionMode mode)

verticalHeaderFormat : VerticalHeaderFormat

This property holds the format of the vertical header.

The default value is QCalendarWidget::ISOWeekNumber.

Access functions:

QCalendarWidget::VerticalHeaderFormat verticalHeaderFormat() const
void setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat format)