Search code examples
qtqt5qwidget

Change disabled QCalendarWidget weekends color


I want to customize QCalendarWidget and I can't change the weekends colors for the disabled state. This is how it looks right now:

enter image description here

I would like to gray out the red. I know that you can set the weekends colors with:

QTextCharFormat weekendFormat;
weekendFormat.setForeground(QBrush(Qt::green, Qt::SolidPattern));
m_ui->calendarWidget->setWeekdayTextFormat(Qt::Saturday, weekendFormat);
m_ui->calendarWidget->setWeekdayTextFormat(Qt::Sunday, weekendFormat);

but this doesn't affect the disabled state. How can I affect the disabled state and set different disabled colors for the weekend?

Thanks!


Solution

  • If you want to get different colors for enabled and disabled states, you can subclass and reimplement the change event handler:

    void MyCalendar::changeEvent(QEvent *event)
    {
        QCalendarWidget::changeEvent(event);
        if (event->type() == QEvent::EnabledChange)
        {
            QColor color;
    
            if (isEnabled())
            {
                color = Qt::blue;
            }
            else
            {
                color = Qt::yellow;
            }
    
            QTextCharFormat weekendFormat;
            weekendFormat.setForeground(QBrush(color, Qt::SolidPattern));
            setWeekdayTextFormat(Qt::Saturday, weekendFormat);
            setWeekdayTextFormat(Qt::Sunday, weekendFormat);
        }
    }