The QCalendarWidget displays a monthly calendar which allows a user a convenient way to select a date. This example code shows how to set the minimum and maximum date ranges for a calendar.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
private:
void minDateChanged(const QDate &date);
void maxDateChanged(const QDate &date);
void selectedDateChanged();
QCalendarWidget *calendar;
QDateEdit *minCalendarDate;
QDateEdit *maxCalendarDate;
QDateEdit *currentDate;
};
This declaration for MainWindow declares three slot methods and three date edit widgets which will be used by the corresponding slot methods. We also declare the calendar widget.
MainWindow::MainWindow()
{
setMinimumSize(700, 350);
calendar = new QCalendarWidget;
calendar->setMinimumDate(QDate(1950, 1, 1));
calendar->setMaximumDate(QDate(2100, 1, 1));
calendar->setGridVisible(true);
QLabel *minLabel = new QLabel("Minimum Date:");
minCalendarDate = new QDateEdit;
minCalendarDate->setDisplayFormat("MMM d yyyy");
minCalendarDate->setDate(calendar->minimumDate());
minCalendarDate->setDateRange(
calendar->minimumDate(), calendar->maximumDate());
QLabel *maxLabel = new QLabel("Maximum Date:");
maxCalendarDate = new QDateEdit;
maxCalendarDate->setDisplayFormat("MMM d yyyy");
maxCalendarDate->setDate(calendar->maximumDate());
maxCalendarDate->setDateRange(
calendar->minimumDate(), calendar->maximumDate());
QLabel *currentLabel = new QLabel("Current Date:");
currentDate = new QDateEdit();
currentDate->setDisplayFormat("MMM d yyyy");
currentDate->setDate(calendar->selectedDate());
currentDate->setDateRange(
calendar->minimumDate(), calendar->maximumDate());
QPushButton *pb_1 = new QPushButton();
pb_1->setText("Close");
QGridLayout *grid1 = new QGridLayout();
grid1->setContentsMargins(75, 45, 75, 25);
grid1->addWidget(calendar, 0, 0, 1, 4);
grid1->addItem(new QSpacerItem(0, 25), 1, 0, 1, 4);
grid1->addWidget(minLabel, 2, 1);
grid1->addWidget(minCalendarDate, 2, 2);
grid1->addWidget(maxLabel, 3, 1);
grid1->addWidget(maxCalendarDate, 3, 2);
grid1->addWidget(currentLabel, 4, 1);
grid1->addWidget(currentDate, 4, 2);
QHBoxLayout *layout1 = new QHBoxLayout();
layout1->addStretch();
layout1->addWidget(pb_1);
layout1->addStretch();
QVBoxLayout *layoutMain = new QVBoxLayout(this);
layoutMain->addLayout(grid1);
layoutMain->addSpacing(25);
layoutMain->addLayout(layout1);
connect(minCalendarDate, &QDateEdit::dateChanged, this,
&MainWindow::minDateChanged);
connect(maxCalendarDate, &QDateEdit::dateChanged, this,
&MainWindow::maxDateChanged);
connect(calendar, &QCalendarWidget::selectionChanged, this,
&MainWindow::selectedDateChanged);
connect(currentDate, &QDateEdit::dateChanged, calendar,
&QCalendarWidget::setSelectedDate);
connect(pb_1, &QPushButton::clicked, this, &QWidget::close);
}
This code sets up a user interface to display a calendar widget with three date edit controls below it. The first two date widgets allow the user to adjust the min and max values for the calendar. The last date widget is used to enter a valid date.
The calendar widget is automatically displayed with arrows in the navigation bar to move backward or forward by a month. There is a drop down arrow next to the month name. Clicking this arrow shows a list of all the months and the user can select one. The year is actually a spin box. Users can type a valid year or use the up and down arrows to move one year at a time.
On line 35 there is a call to setContentsMargins() which will add space around grid. The four numbers are left, top, right, and bottom. So our code is setting a top margin of 45 and a bottom margin of 25.
The calendar widget is added to the grid layout on line 37 with four integers specifying its position in the grid. The first two numbers indicate the starting row and column, both of which are zero. The third and fourth numbers indicate how many rows and columns to span. So the calendar widget will appear in row zero and span all four columns.
Line 38 shows how to add a blank row which adds some distance between the calendar and the “Minimum Date”. The second row in the grid will contain a 25 pixel vertical spacer spanning all four columns.
Signal / Slot Connections
Line 68 sets up a connection which is very interesting. When the user selects a new date in the calendar widget, this Signal / Slot connection will update the “Current Date” control.
void MainWindow::minDateChanged(const QDate &date)
{
calendar->setMinimumDate(date);
currentDate->setDateRange(
calendar->minimumDate(), calendar->maximumDate());
}
void MainWindow::maxDateChanged(const QDate &date)
{
calendar->setMaximumDate(date);
currentDate->setDateRange(
calendar->minimumDate(), calendar->maximumDate());
}
void MainWindow::selectedDateChanged()
{
currentDate->setDate(calendar->selectedDate());
}
The first two slots will modify the minimum and maximum date range for the calendar.
The selectedDateChanged() slot method is called when the users changes the date in the “Current Date” control. The code on line 17 will move the calendar to the corresponding date.
Main Function
Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.
Running the Example
To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_14”.