QCalendarWidget Class
The QCalendarWidget class shows a month and lets the user pick a date. More…
| Header | #include <QCalendarWidget> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QWidget |
List of all members, including inherited members
Public Types
| enum | SelectionMode |
| enum | VerticalHeaderFormat |
Public Functions
| QCalendarWidget(QWidget *parent = nullptr) | |
| ~QCalendarWidget() | |
| QDate | selectedDate() const |
| int | yearShown() const |
| int | monthShown() const |
| QDate | minimumDate() const |
| void | setMinimumDate(QDate date) |
| QDate | maximumDate() const |
| void | setMaximumDate(QDate date) |
| Qt::DayOfWeek | firstDayOfWeek() const |
| void | setFirstDayOfWeek(Qt::DayOfWeek dayOfWeek) |
| bool | isGridVisible() const |
| SelectionMode | selectionMode() const |
| void | setSelectionMode(SelectionMode mode) |
| VerticalHeaderFormat | verticalHeaderFormat() const |
| void | setVerticalHeaderFormat(VerticalHeaderFormat format) |
| bool | isNavigationBarVisible() const |
Reimplemented Public Functions
| QSize | sizeHint() const |
| QSize | minimumSizeHint() const |
Public Slots
| void | setSelectedDate(QDate date) |
| void | setDateRange(QDate min, QDate max) |
| void | setCurrentPage(int year, int month) |
| void | setGridVisible(bool show) |
| void | setNavigationBarVisible(bool visible) |
| void | showNextMonth() |
| void | showPreviousMonth() |
| void | showNextYear() |
| void | showPreviousYear() |
| void | showSelectedDate() |
| void | showToday() |
Signals
| void | selectionChanged() |
| void | clicked(QDate date) |
| void | activated(QDate date) |
| void | currentPageChanged(int year, int month) |
Reimplemented Protected Functions
| void | paintEvent(QPaintEvent *event) |
| void | resizeEvent(QResizeEvent *event) |
| void | mousePressEvent(QMouseEvent *event) |
| void | mouseDoubleClickEvent(QMouseEvent *event) |
| void | keyPressEvent(QKeyEvent *event) |
| void | wheelEvent(QWheelEvent *event) |
| void | changeEvent(QEvent *event) |
Detailed Description
A calendar shows the days of one month in a grid of weeks, with the days before and after it in gray, and a bar above it with the month and the year and buttons for the previous and the next month:
auto *calendar = new QCalendarWidget(window);
calendar->setDateRange(QDate::currentDate(), QDate::currentDate().addYears(1));
connect(calendar, &QCalendarWidget::clicked, [](QDate date) { book(date); });
The user clicks a day, turns the mouse wheel to change the month, or uses the arrow keys (a day or a week), Page Up and Page Down (a month), Home and End (the first and last day of the month) and Return. Dates outside the range cannot be selected.
Differences from Qt
The names of the months and the days are English and the first day of the week is Monday, as there is no QLocale; setFirstDayOfWeek() changes it. There are no date text formats (setDateTextFormat()), no horizontalHeaderFormat(), no month and year menus in the navigation bar, and no dateEditEnabled().
Member Type Documentation
enum QCalendarWidget::SelectionMode
Whether the user can select a date.
| Constant | Description |
|---|---|
QCalendarWidget::NoSelection | The selected date cannot be changed by the user. |
QCalendarWidget::SingleSelection | The user can select one date. |
enum QCalendarWidget::VerticalHeaderFormat
What is shown at the left of the weeks.
| Constant | Description |
|---|---|
QCalendarWidget::NoVerticalHeader | Nothing. |
QCalendarWidget::ISOWeekNumbers | The ISO 8601 numbers of the weeks. |
Member Function Documentation
QCalendarWidget::QCalendarWidget(QWidget *parent = nullptr)
Constructs a calendar with the given parent. Today is selected and its month is shown; the range is from 1752-09-14 to 9999-12-31, as in Qt.
QCalendarWidget::~QCalendarWidget()
Destroys the calendar.
void QCalendarWidget::activated(QDate date)
This signal is emitted when the user double-clicks the date or presses Return or Enter on it.
void QCalendarWidget::changeEvent(QEvent *event)
Reimplements QWidget::changeEvent().
void QCalendarWidget::clicked(QDate date)
This signal is emitted when the user clicks the date, if it is in the range.
void QCalendarWidget::currentPageChanged(int year, int month)
This signal is emitted when the calendar shows another month: the month of the year.
Qt::DayOfWeek QCalendarWidget::firstDayOfWeek() const
Returns the day in the first column, Qt::Monday by default.
bool QCalendarWidget::isGridVisible() const
Returns whether lines are drawn between the days. The default is false.
bool QCalendarWidget::isNavigationBarVisible() const
Returns whether the navigation bar is shown. The default is true.
void QCalendarWidget::keyPressEvent(QKeyEvent *event)
Reimplements QWidget::keyPressEvent().
The arrow keys move the selection by a day or a week, Page Up and Page Down by a month, Home and End to the first and last day of the month. Return and Enter emit activated().
QDate QCalendarWidget::maximumDate() const
Returns the last date that can be selected.
See also setDateRange().
QDate QCalendarWidget::minimumDate() const
Returns the first date that can be selected.
See also setDateRange().
QSize QCalendarWidget::minimumSizeHint() const
Reimplements QWidget::minimumSizeHint().
int QCalendarWidget::monthShown() const
Returns the month that is shown, from 1 to 12.
void QCalendarWidget::mouseDoubleClickEvent(QMouseEvent *event)
Reimplements QWidget::mouseDoubleClickEvent().
void QCalendarWidget::mousePressEvent(QMouseEvent *event)
Reimplements QWidget::mousePressEvent().
A click on a date in the range selects it and emits clicked(); a date of another month shows that month.
void QCalendarWidget::paintEvent(QPaintEvent *event)
Reimplements QWidget::paintEvent().
void QCalendarWidget::resizeEvent(QResizeEvent *event)
Reimplements QWidget::resizeEvent().
QDate QCalendarWidget::selectedDate() const
Returns the selected date.
See also setSelectedDate().
void QCalendarWidget::selectionChanged()
This signal is emitted when the selected date changes, by the user or by setSelectedDate().
SelectionMode QCalendarWidget::selectionMode() const
Returns whether the user can select a date, SingleSelection by default.
void QCalendarWidget::setCurrentPage(int year, int month)
Shows the month of the year, without changing the selected date, and emits currentPageChanged().
void QCalendarWidget::setDateRange(QDate min, QDate max)
Sets the range of dates that can be selected to min to max, and brings the selected date into it.
void QCalendarWidget::setFirstDayOfWeek(Qt::DayOfWeek dayOfWeek)
Sets the day in the first column to dayOfWeek.
void QCalendarWidget::setGridVisible(bool show)
Sets whether lines are drawn between the days to show.
void QCalendarWidget::setMaximumDate(QDate date)
Sets the last date that can be selected to date. The first date falls to it if it was later, and the selected date is brought into the range.
void QCalendarWidget::setMinimumDate(QDate date)
Sets the first date that can be selected to date. The last date grows to it if it was earlier, and the selected date is brought into the range.
void QCalendarWidget::setNavigationBarVisible(bool visible)
Shows the navigation bar if visible is true, and hides it otherwise.
void QCalendarWidget::setSelectedDate(QDate date)
Selects date if it is in the range, shows its month, and emits selectionChanged() if it changes.
See also selectedDate().
void QCalendarWidget::setSelectionMode(SelectionMode mode)
Sets whether the user can select a date to mode.
void QCalendarWidget::setVerticalHeaderFormat(VerticalHeaderFormat format)
Sets what is shown at the left of the weeks to format.
void QCalendarWidget::showNextMonth()
Shows the month after the one that is shown.
void QCalendarWidget::showNextYear()
Shows the month of the next year.
void QCalendarWidget::showPreviousMonth()
Shows the month before the one that is shown.
void QCalendarWidget::showPreviousYear()
Shows the month of the previous year.
void QCalendarWidget::showSelectedDate()
Shows the month of the selected date.
void QCalendarWidget::showToday()
Shows the month of today.
QSize QCalendarWidget::sizeHint() const
Reimplements QWidget::sizeHint().
VerticalHeaderFormat QCalendarWidget::verticalHeaderFormat() const
Returns what is shown at the left of the weeks, ISOWeekNumbers by default.
void QCalendarWidget::wheelEvent(QWheelEvent *event)
Reimplements QWidget::wheelEvent().
The wheel shows the previous or the next month.
int QCalendarWidget::yearShown() const
Returns the year of the month that is shown.