Search code examples
c++qtqt5qcalendarwidget

Painter error in paintCell() function when subclassing QCalendarWidget


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


Solution

  • You should not draw directly since the paintCell method is called internally, it is appropriate to save the dates in a list, and if the date used by paintCell is contained in that list, draw it in a personalized way:

    #ifndef MYCALENDAR_H
    #define MYCALENDAR_H
    
    #include <QCalendarWidget>
    #include <QPainter>
    class MyCalendar : public QCalendarWidget
    {
    public:
        MyCalendar(QWidget *parent=Q_NULLPTR):QCalendarWidget{parent}{
    
        }
        void addHoliday(const QDate &date){
            mDates<<date;
            updateCell(date);
        }
        void paintCell(QPainter * painter, const QRect & rect, const QDate & date) const{
            if(mDates.contains(date)){
                painter->save();
                painter->setBrush(Qt::red);
                painter->drawRect(rect);
                painter->drawText(rect, Qt::AlignCenter|Qt::TextSingleLine, QString::number(date.day()));
                painter->restore();
            }
            else
                QCalendarWidget::paintCell(painter, rect, date);
    
        }
    private:
        QList<QDate> mDates;
    };
    
    #endif // MYCALENDAR_H