What I'm trying to do is to apply custom CSS to a custom widget deriving from QLabel but I'm having no luck.
I have the custom class defined as:
class CustomLabel : public QLabel {
}
I've not re-implemented the paintEvent function as I thought that given that the standard QLabel supports CSS stylesheets, I would just need to refer to this new widget in CSS, for example:
CustomLabel {
background-color: #111111;
font-size: 18px;
font-weight: bold;
}
Unfortunately at run-time, no style is applied the CustomLabel and the default QLabel style is applied.
Can anyone suggest why my CSS rules are being ignored for CustomLabel?
Steps to Recreate
- Create a Qt Widgets project using Qt Creator
- Add a custom class derived from
QLabeland call itCustomLabel - Add a
QLabelonto a form using the designer - Promote the label to a
CustomLabelclass Apply the stylesheet using the following code in
main.cpp:a.setStyleSheet("CustomLabel {font-weight: bold;}");Run the program and notice how
CustomLabelis not styled in accordance with the CSS style.