Search code examples
qtbackgroundborderstylesheetqwidget

Borders and background of QWidget aren't set by stylesheet


I've set the stylesheet of a QWidget to change the borders and the background,

#gui {
    border: 4px inset #515c84;
    border-radius: 9px;
    background-image: url(./back.png)
}

Its name is gui but neither border nor background are shown.


Solution

  • Override paintEvent in your QWidget subclass like this:

    void MyWidget::paintEvent(QPaintEvent *e)
    {
        QStyleOption opt;
        opt.init(this);
        QStylePainter p(this);
        p.drawPrimitive(QStyle::PE_Widget, opt);
    }