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 change the colors for various days of the week.
class MainWindow : public QWidget
{
public:
MainWindow();
private:
void weekdayFormatChanged();
void weekendFormatChanged();
QComboBox *createColorComboBox();
QCalendarWidget *calendar;
QComboBox *weekdayColor;
QComboBox *weekendColor;
};
This declaration for MainWindow declares two slot methods and two combo boxes which will be used by the corresponding slot methods to update the colors of the calendar. We also declare a color selection combo box and 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 *weekdayColorLabel = new QLabel("Monday to Friday:");
weekdayColor = createColorComboBox();
weekdayColor->setCurrentIndex(
weekdayColor->findText("Black"));
//
QLabel *weekendColorLabel = new QLabel("Saturday, Sunday:");
weekendColor = createColorComboBox();
weekendColor->setCurrentIndex(
weekendColor->findText("Red"));
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(weekdayColorLabel, 2, 1);
grid1->addWidget(weekdayColor, 2, 2, 1, 2);
grid1->addWidget(weekendColorLabel, 3, 1);
grid1->addWidget(weekendColor, 3, 2, 1, 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(weekdayColor,
cs_mp_cast<int>(&QComboBox::currentIndexChanged),
this, &MainWindow::weekdayFormatChanged);
connect(weekendColor,
cs_mp_cast<int>(&QComboBox::currentIndexChanged),
this, &MainWindow::weekendFormatChanged);
connect(pb_1, &QPushButton::clicked,
this, &QWidget::close);
}
The code for this user interface is very similar to what has been shown in examples 14 and 15.
The variable weekdayColor is a pointer to a QComboBox. The combo box items are populated by calling createColorComboBox() on line 13. The code for this method is shown below. On line 14 there is a call to setCurrentIndex() which selects an item from the combo box. When this control is displayed the default selection will be the color “Black”. The same process is used on lines 20 and 21 however the default color for this combo box will be “Red”.
The two calls to connect() on lines 51 and 55 will result in color changes in the calendar based on the users selections.
Signal / Slot Connections
Lines 51 and 55 are calls to the connect() method. The parameters indicate what action should be taken when the values of the two drop down combo boxes are changed.
void MainWindow::weekdayFormatChanged()
{
QTextCharFormat format;
format.setForeground(
weekdayColor->itemData(weekdayColor->currentIndex())
.value<QColor>());
calendar->setWeekdayTextFormat(Qt::Monday, format);
calendar->setWeekdayTextFormat(Qt::Tuesday, format);
calendar->setWeekdayTextFormat(Qt::Wednesday, format);
calendar->setWeekdayTextFormat(Qt::Thursday, format);
calendar->setWeekdayTextFormat(Qt::Friday, format);
}
void MainWindow::weekendFormatChanged()
{
QTextCharFormat format;
format.setForeground(
weekendColor->itemData(weekendColor->currentIndex())
.value<QColor>());
calendar->setWeekdayTextFormat(Qt::Saturday, format);
calendar->setWeekdayTextFormat(Qt::Sunday, format);
}
QComboBox *MainWindow::createColorComboBox()
{
QComboBox *comboBox = new QComboBox;
comboBox->addItem("Black", QColor(Qt::black));
comboBox->addItem("Blue", QColor(Qt::blue));
comboBox->addItem("Cyan", QColor(Qt::cyan));
comboBox->addItem("Green", QColor(Qt::green));
comboBox->addItem("Magenta", QColor(Qt::magenta));
comboBox->addItem("Red", QColor(Qt::red));
return comboBox;
}
In the first slot line 4 will set the foreground color for the days of the week ranging from Monday through Friday. In the following slot method the foreground color is changed on line 18 for Saturday and Sunday. It is worth mentioning this code can be enhanced to query the user’s locale to determine which days of the week are considered weekdays versus the weekend.
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_16”.