Using QStylesheet in Custom Style -> wrong palette.

  • Hello all,

    I use Qt4.8.4 to build an desktop application. I want to use QStyleSheet to apply a custom look and feel to my application. However I also need to draw custom ui elements in a MyStyle (which inherits from QWindowsVistaStyle). I want to style them with the values from my QStyleSheet.

    What works:
    I set the QStyleSheet to my QApplication, it gets correctly applied on my QWidgets as long as I paint default elements through MyStyle.
    But requesting the palette and painting i.e. a rectangle by my self I still get the default style configuration.

    QWidget {
    background: #e18918;

    Some draw.
    void MyStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *opt,
    QPainter *p, const QWidget *w) const

    qDebug() << opt->palette.background().color;


    Painting the item with the default element provides me the correct orange color, however the background color in opt is still default gray.

    How can I read / get the correct value from the QStyleSheet?



  • Ok,

    i checked some code... it looks like that you can not use CSS Styles within custom widgets...

    @ if (QStyleSheetStyle* cssStyle = qobject_cast<QStyleSheetStyle*>(style)) {

            cssStyle->styleSheetPalette(this, &opt, &opt.palette);


    seems to be the magic, but QStyleSheetStyle is only private, is there any public interface?



Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.