如何自定义QCalendarWidget?

时间:2019-07-16 03:53:04

标签: python python-3.x pyqt5 pyside2 qcalendarwidget

我正在尝试将一些样式表应用于QCalendarWidget,并且已经进行了一些更改。这是我目前的代码:

QCalendarWidget QWidget{
background-color:magenta;
color: green;

}

QCalendarWidget QToolButton{
background-color:black;
color: green;
icon-size: 30px;
}

QCalendarWidget QMenu{
background-color:magenta;
color: green;

}

QCalendarWidget QAbstractItemView:enabled{
background-color: yellow;
color: black;
}

QCalendarWidget QAbstractItemView:disabled{
background-color: yellow;
color: white;
}

QCalendarWidget QMenu{
    background-color: rgb(255, 46, 221);
}

QCalendarWidget QSpinBox{
    background-color: black;
}

结果是这样的:

image 1

问题是我找不到如何自定义绿色箭头,星期几和星期几。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

这仅适用于月份按钮,有点hacky,请记住,它基于硬编码的私有方法。它使用objectName获取特定的QToolButton(“ qt_calendar_prevmonth”和“ qt_calendar_nextmonth”)并设置图标。不要使用image: url,因为它不适用于QToolButtons。

从理论上讲,您还可以通过遍历标题的布局并检查第一个和最后一个QToolButtons来找到它们的对象名称,这可能有点“困难”,但可能更安全,然后,您可以相应地设置样式表

#qt_calendar_prevmonth {
    background-color: blue;
    icon-size: 30px;
    qproperty-icon: url(prevmonth.svg);
}
#qt_calendar_nextmonth {
    background-color: orange;
    icon-size: 30px;
    qproperty-icon: url(nextmonth.svg);
}

不幸的是,由于QCalendarWidget的工作方式,无法通过样式表更改“标头”的格式,因此您需要坚持使用QCalendarWidget的headerTextFormat(),dateTextFormat()等。

fmt = cal.headerTextFormat()
fmt.setForeground(QtGui.QColor('green'))
fmt.setBackground(QtGui.QColor('orange'))
cal.setHeaderTextFormat(fmt)