I want to create calendar, that will mark several dates entered by user. So I have subclassed QCalendarWidget and reimplemented painCell function. Here is my simplified code:
MyCalendar::MyCalendar(QWidget *parent)
: QCalendarWidget(parent)
{
painter = new QPainter(this);
}
void MyCalendar::setHolidays(QDate date)
{
paintCell(painter, rect(),date);
}
void MyCalendar::paintCell(QPainter * painter, const QRect & rect, const QDate & date) const
{
painter->setBrush(Qt::red);
QCalendarWidget::paintCell(painter, rect, date);
}
I can't do it though, because when creating QPainter object i am getting this message: "QWidget::paintEngine: Should no longer be called QPainter::begin: Paint device returned engine == 0, type: 1"
When I am not setting painter parent, I get this error when trying to set brush: "QPainter::setBrush: Painter not active" I think, I am creating QPainter object in a wrong place. Anyone knows, how to resolve this?
I was using Qt wiki snippet: https://wiki.qt.io/How_to_create_a_custom_calender_widget
painter.setBrush(Qt::red);orpainter->setBrush(Qt::red);? - eyllanescpaintCell()insetHolidays()might be the wrong way. Instead, IMHOsetHolydays()should callupdateCells()(orupdateCell()for a specific date). ThepaintCell()is a virtual method which is called out of thepaint()method (inherited fromQWidget). It should check for a special day to modify appearance. It probably gets aQPainterinstance as argument which is managed insideQCalendarWidget- no need to create your own. I just see the answer of eyllanesc. It seems he had the same idea... - Scheff's Cat