如何将 qss 应用于 QCalendarWidget?

How to apply qss to a QCalendarWidget?

晚上好,我正在尝试生成如图所示的日历:

到目前为止,我使用 QSS 取得的成果是:

这是我用过的QSS

QCalendarWidget QAbstractItemView
{ 
selection-background-color: #042944; 
selection-color: white;
selection-border:10px solid red;

}
QCalendarWidget QWidget 
{
  color:grey;
}
QCalendarWidget QTableView{
border-width:0px;
background-color:lightgrey;
}

我还需要更改日期(星期日和星期六)的颜色,并且选择日期时边缘是圆的。

这是我试过的:

QCalendarWidget{
   color:grey;
}

QCalendarWidget QTableView::item:selected{
   border-radius:30px;

}

但是不行希望大家帮帮我

并不是所有的事情都可以用 Qt Style Sheet,事实上它是非常有限的,在这种情况下改变周末的颜色你必须使用 setWeekdayTextFormat() 并且改变形状您应该使用的选定日期 paintCell().

from PyQt5 import QtCore, QtGui, QtWidgets

QSS = '''
QCalendarWidget QAbstractItemView
{ 
    selection-background-color: #042944; 
    selection-color: white;
}
QCalendarWidget QWidget 
{
  color:grey;
}
QCalendarWidget QTableView
{
    border-width:0px;
    background-color:lightgrey;
}
'''

class CalendarWidget(QtWidgets.QCalendarWidget):
    def __init__(self, parent=None):
        super(CalendarWidget, self).__init__(parent,
            verticalHeaderFormat=QtWidgets.QCalendarWidget.NoVerticalHeader,
            gridVisible=False)

        for d in (QtCore.Qt.Saturday, QtCore.Qt.Sunday,):
            fmt = self.weekdayTextFormat(d)
            fmt.setForeground(QtCore.Qt.darkGray)
            self.setWeekdayTextFormat(d, fmt)

    def paintCell(self, painter, rect, date):
        if date == self.selectedDate():
            painter.save()
            painter.fillRect(rect, QtGui.QColor("#D3D3D3"))
            painter.setPen(QtCore.Qt.NoPen)
            painter.setBrush(QtGui.QColor("#33B5E5"))
            r = QtCore.QRect(QtCore.QPoint(), min(rect.width(), rect.height())*QtCore.QSize(1, 1))
            r.moveCenter(rect.center())
            painter.drawEllipse(r)
            painter.setPen(QtGui.QPen(QtGui.QColor("gray")))
            painter.drawText(rect, QtCore.Qt.AlignCenter, str(date.day()))
            painter.restore()
        else:
            super(CalendarWidget, self).paintCell(painter, rect, date)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setStyleSheet(QSS)
    w = CalendarWidget()
    w.show()
    sys.exit(app.exec_())