Qt Stylesheet for custom widget

I had a similar problem and it was solved using jecjackal’s comment. As sjwarner said, it would be much more noticeable in the form of an answer. So I’ll provide it. For the benefit of any future viewers. Again, it isn’t my answer! Appreciate jecjackal for it!

As it is said in the Qt’s stylesheets reference, applying CSS styles to custom widgets inherited from QWidget requires reimplementing paintEvent() in that way:

 void CustomWidget::paintEvent(QPaintEvent *)
 {
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
 }

Without doing it your custom widgets will support only the background, background-clip and background-origin properties.

You can read about it here: Qt Stylesheets reference in the section “List of Stylable Widgets” -> QWidget.

Leave a Comment