Piontk
Piontk

Reputation: 93

How to customize 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;
}

And the result is this:

image 1

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?

Upvotes: 1

Views: 4069

Answers (1)

musicamante
musicamante

Reputation: 48231

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.

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

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)

Upvotes: 3

Related Questions