Qt 更改禁用的QCalendarWidget颜色

Qt 更改禁用的QCalendarWidget颜色,qt,qt5,qwidget,Qt,Qt5,Qwidget,我想自定义QCalendarWidget,无法更改禁用状态的周末颜色。这就是它现在的样子: 我想把红色调成灰色。我知道您可以使用以下设置周末颜色: QTextCharFormat weekendFormat; weekendFormat.setForeground(QBrush(Qt::green, Qt::SolidPattern)); m_ui->calendarWidget->setWeekdayTextFormat(Qt::Saturday, weekendFormat);

我想自定义
QCalendarWidget
,无法更改禁用状态的周末颜色。这就是它现在的样子:

我想把红色调成灰色。我知道您可以使用以下设置周末颜色:

QTextCharFormat weekendFormat;
weekendFormat.setForeground(QBrush(Qt::green, Qt::SolidPattern));
m_ui->calendarWidget->setWeekdayTextFormat(Qt::Saturday, weekendFormat);
m_ui->calendarWidget->setWeekdayTextFormat(Qt::Sunday, weekendFormat);
但这不会影响禁用状态。如何影响禁用状态并为周末设置不同的禁用颜色


谢谢

如果要为启用和禁用状态获取不同的颜色,可以对更改事件处理程序进行子类化并重新实现:

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);
    }
}

我使用Qt 4.6.2,您的代码对我来说很好。@hank代码更改启用状态的颜色。禁用状态下的红色是否也在变化?是的,新颜色也适用于禁用的项目。是的,但我想为禁用和启用状态设置不同的颜色。如果不重新实现paint事件,这可能吗?是的,我认为如果不重新实现
QCalendarWidget
,就不可能实现它。。。这就是解决方案