You should create your own custom calendar class derived from QCalendarWidget
and reimplement void QCalendarWidget::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
method.
So let's create a Calendar
class in the Calendar.h:
#include <QCalendarWidget>
class Calendar : public QCalendarWidget
{
public:
Calendar(QWidget *parent = nullptr);
void paintCell(QPainter *painter, const QRect &rect, const QDate &date) const override;
};
Implement this class in Calendar.cpp:
#include "Calendar.h"
#include <QPainter>
Calendar::Calendar(QWidget *parent)
: QCalendarWidget(parent)
{}
void Calendar::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
// change color for dates before current date
if (date < QDate::currentDate())
{
painter->save();
// set color for the text
painter->setPen(QColor(64, 64, 64));
// draw text with new color
painter->drawText(rect, Qt::TextSingleLine | Qt::AlignCenter, QString::number(date.day()));
// here you can draw anything you want
painter->restore();
} else {
// draw cell in standard way
QCalendarWidget::paintCell(painter, rect, date);
}
}
And after that if you're using Qt Creator, you can add QCalendarWidget
from the left bar to your widget, right click on it and open Promote to menu. Add Calendar
to Promoted class name and check that Header file is correct. Hit Add to add Calendar
to the promoted classes list and finally hit Promote to turn the QCalendarWidget
on your form into a Calendar
.
After that I get the following widget (sorry for Russian):
