PDA

View Full Version : Inherited Style Sheet



johnmauer
2nd January 2010, 18:44
I have been unable to directly change a style sheet in a class inherited from QWidget, although it works when the class is QWidget. The simplified code that does not work (nothing in constructor):


class Banner : public QWidget
{
Q_OBJECT
public:
Banner(QWidget *parent=0);
~Banner();
};

banner = new Banner;
banner->setStyleSheet("background-color: rgb(0, 100, 200);");


While this does work:


banner = new QWidget;
banner->setStyleSheet("background-color: rgb(0, 100, 200);");


I'm sure to have missed some understanding here and would appreciate help. Thanks

johnmauer
2nd January 2010, 21:07
Now if only I could read ALL the documentation!

If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:


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

It works fine now...