The QCalendarWidget displays a monthly calendar which allows a user a convenient way to select a date. In this example we will show how to modify which day of the week the calendar should start on and various other appearance settings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class MainWindow : public QWidget { public : MainWindow(); private : void firstDayChanged( int index); void horizontalHeaderChanged( int index); void verticalHeaderChanged( int index); QCalendarWidget *calendar; QComboBox *firstDayCombo; QComboBox *horizontalCombo; QComboBox *verticalCombo; QCheckBox *gridCheckBox; QCheckBox *navCheckBox; }; |
This declaration for MainWindow declares three slot methods and five other widgets which will be used by the corresponding slot methods to update the appearance of the calendar. We also declare the calendar widget.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | MainWindow::MainWindow() { setMinimumSize(700, 350); calendar = new QCalendarWidget; calendar->setMinimumDate(QDate(1950, 1, 1)); calendar->setMaximumDate(QDate(2100, 1, 1)); calendar->setGridVisible( true ); QLabel *firstDayLabel = new QLabel( "Week starts on:" ); firstDayCombo = new QComboBox; firstDayCombo->addItem( "Sunday" , Qt::Sunday); firstDayCombo->addItem( "Monday" , Qt::Monday); firstDayCombo->addItem( "Tuesday" , Qt::Tuesday); firstDayCombo->addItem( "Wednesday" , Qt::Wednesday); firstDayCombo->addItem( "Thursday" , Qt::Thursday); firstDayCombo->addItem( "Friday" , Qt::Friday); firstDayCombo->addItem( "Saturday" , Qt::Saturday); // QLabel *horizontalLabel = new QLabel( "Horizontal header:" ); horizontalCombo = new QComboBox; horizontalCombo->addItem( "Short day names" , QCalendarWidget::ShortDayNames); horizontalCombo->addItem( "Single letter day names" , QCalendarWidget::SingleLetterDayNames); horizontalCombo->addItem( "None" , QCalendarWidget::NoHorizontalHeader); horizontalCombo->setCurrentIndex(0); // QLabel *verticalLabel = new QLabel( "Vertical header:" ); verticalCombo = new QComboBox; verticalCombo->addItem( "Show Week Numbers" , QCalendarWidget::ISOWeekNumbers); verticalCombo->addItem( "None" , QCalendarWidget::NoVerticalHeader); gridCheckBox = new QCheckBox( "&Show Grid" ); gridCheckBox->setChecked(calendar->isGridVisible()); navCheckBox = new QCheckBox( "&Navigation bar" ); navCheckBox->setChecked( true ); QPushButton *pb_1 = new QPushButton(); pb_1->setText( "Close" ); // QGridLayout *grid1 = new QGridLayout(); grid1->setContentsMargins(75, 45, 75, 25); grid1->setHorizontalSpacing(35); grid1->addWidget(calendar, 0, 0, 1, 5); grid1->addItem( new QSpacerItem(0, 25), 1, 0, 1, 5); grid1->addWidget(firstDayLabel, 2, 1); grid1->addWidget(firstDayCombo, 2, 2, 1, 2); grid1->addWidget(horizontalLabel, 3, 1); grid1->addWidget(horizontalCombo, 3, 2, 1, 2); grid1->addWidget(verticalLabel, 4, 1); grid1->addWidget(verticalCombo, 4, 2, 1, 2); grid1->addWidget(gridCheckBox, 5, 2); grid1->addWidget(navCheckBox, 5, 3); 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(firstDayCombo, cs_mp_cast< int >(&QComboBox::currentIndexChanged), this , &MainWindow::firstDayChanged); connect(horizontalCombo, cs_mp_cast< int >(&QComboBox::currentIndexChanged), this , &MainWindow::horizontalHeaderChanged); connect(verticalCombo, cs_mp_cast< int >(&QComboBox::currentIndexChanged), this , &MainWindow::verticalHeaderChanged); connect(gridCheckBox, &QCheckBox::toggled, calendar, &QCalendarWidget::setGridVisible); connect(navCheckBox, &QCheckBox::toggled, calendar, &QCalendarWidget::setNavigationBarVisible); connect(pb_1, &QPushButton::clicked, this , &QWidget::close); } |
The user interface displays a calendar widget with three drop down boxes. The first one allows the user to specify the first day of the week. The second one changes the header from the short names such as “Sun” or “Mon”, a single letter “S” or “M”, or the days of the week removed completely. The third combo box allows the user to display or remove the week number, located on the left side of the calendar.
The two check boxes located at the bottom of the dialog are used to enable or disable the grid and navigation bar.
Details about setContentsMargins() and QSpacerItem() were provided in example 14. The setHorizontalSpacing() on line 55 adjusts the spacing between all of the columns. Extra spacing of 35 pixels will appear before the label text, after the label text, and then after the drop down controls.
Signal / Slot Connections
The first three calls to connect() use the cs_mp_cast<int> syntax for the Signal method. This is required since the signal methods are overloaded and we need to specify which overload to use.
The connections on line 95 and 98 will modify the appearance of the calendar. The two specified slot methods are part of the QCalendarWidget class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | void MainWindow::firstDayChanged( int index) { calendar->setFirstDayOfWeek( Qt::DayOfWeek(firstDayCombo->itemData(index).toInt())); } void MainWindow::horizontalHeaderChanged( int index) { calendar->setHorizontalHeaderFormat( QCalendarWidget::HorizontalHeaderFormat( horizontalCombo->itemData(index).toInt())); } void MainWindow::verticalHeaderChanged( int index) { calendar->setVerticalHeaderFormat( QCalendarWidget::VerticalHeaderFormat( verticalCombo->itemData(index).toInt())); } |
This block of code shows the implementation for the three slot methods specified in the calls to connect(). When the values for any of the three combo boxes are modified, the appropriate slot method will be invoked.
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_15”.