如何自定义 QCalendarWidget? [英] How to customize QCalendarWidget?

查看:73
本文介绍了如何自定义 QCalendarWidget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to apply some stylesheets to my QCalendarWidget, and I already made some changes. Here is my code at the moment:

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

结果是这样的:

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

The problem is that I can't find how I customize the green arrows, the number of the week, and the days of the week. Any suggestions?

推荐答案

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

This only works for month buttons, is a bit hacky, and keep in mind that it's based on hardcoded private methods. It uses the objectName to get the specific QToolButton ("qt_calendar_prevmonth" and "qt_calendar_nextmonth") and also set the icon. Do not use image: url, as it won't work for QToolButtons.

理论上,您也可以通过浏览标题的布局并检查第一个和最后一个 QToolButton 来找到它们的对象名称,这可能有点难",但可能更安全,然后,您相应地设置样式表.

Theoretically you could also find their object names by going through the layout of the header and check the first and last QToolButtons, which might be a bit "harder", but probably safer, and, after that, you set the stylesheet accordingly.

#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() 等.

Unfortunately, due to the way QCalendarWidget works, there's no way to change the format of "headers" through stylesheet, so you'll need to stick to QCalendarWidget's headerTextFormat(), dateTextFormat() and so on.

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

这篇关于如何自定义 QCalendarWidget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆