QCalendar styling. Attribute for disabled items

1k views Asked by At

In Qt and specifically the QCalendarWidget the disabled(unclickable) dates by default have a grey background. However, I need to style those with a QSS file.

My problem is that I can't find the attribute in the Qt API. For example the items for selection and in general can be styled as such

QAbstractItemView {
    color:#444;
    alternate-background-color: #eee;
    selection-background-color: #0F4A8C;
    selection-color:#fff;
}

Any suggestion how could I set the background color of a disabled item?

2

There are 2 answers

0
MayTheSchwartzBeWithYou On

Apparently, for the dates which are out of range, you can't style them and you need to overwrite the QCalendar's paintCell(painter, rect, date) function like this:

class CustomCalendarWidget(QtGui.QCalendarWidget):
    """ Calendar widget which overwrites the out of date drawing """
    def paintCell(self, painter, rect, date):
        if not self.minimumDate() <= date <= self.maximumDate():
            painter.setBrush(QtGui.QBrush(Qt.lightGray))
            painter.setPen(QtGui.QPen(Qt.lightGray))
            painter.drawRect(rect)
            painter.setPen(QtGui.QPen(Qt.gray))
            painter.drawText(rect, Qt.AlignHCenter | Qt.AlignVCenter, str(date.day()))
        else:
            super(CustomCalendarWidget, self).paintCell(painter, rect, date)
1
Ashot On

This may work.

QAbstractItemView :disabled {
    color:#444;
    alternate-background-color: #eee;
    selection-background-color: #0F4A8C;
    selection-color:#fff;
}

See also here http://doc.qt.io/qt-4.8/stylesheet-examples.html