Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Applying StyleSheet to Custom Window



  • Hi

    I've been looking for an answer to this question on the forum but could not find anything. I am trying to apply a Stylesheet to a promoted widget but it just won't apply.

    I used the Designer interface to promote a QWidget to a QWidget inherited class TitiWidget.
    Still in the designer interface I added

    @
    border: 2px solid green;
    border-radius: 20px;
    padding: 2px;
    background-color: rgb(85, 85, 255);
    @

    to the "StyleSheet" field of the now custom widget.

    The widget's style is changed in the Design window but nothing is changed when I run the application. Demoting the Custom widget to a regular QWidget solves the problem and the style is applied.

    I tried to add TitiWidget#widget or QWidget#widget before the stylesheet arguments. No improvements.

    What should I do ?



  • Styling a custom widget through style sheets is not supported. Sorry.

    Edit:
    Well, let me specify that a bit: custom drawn custom widgets can not be styled using qss. You can use qss to style custom widgets that are just compositions of existing Qt widgets.



  • By custom drawn widget, you do mean widgets inherited from Qwidget, right ?

    Are you aware of any plan to implement this in a next version ?



  • Me? No, not me. I don't work for Nokia :-)

    What I mean by custom drawn, are widgets that are based directly on QWidget and where you implement paintEvent() do do your own painting yourself.



  • If you just derive from QWidget and need that one styled, you have to do some stuff :-)

    @
    void TitiWidget::paintEvent(QPaintEvent* /p_pEvent/)
    {
    QStyleOption opt;
    opt.init(this);
    QStylePainter p(this);
    p.drawPrimitive(QStyle::PE_Widget, opt);
    }
    @



  • why not in the constructor of TitiWidget put this ?

    @
    setStyleSheet(
    "border: 2px solid green;"
    "border-radius: 20px;"
    "padding: 2px;"
    "background-color: rgb(85, 85, 255);"
    );@



  • [quote author="marcoB" date="1305200019"]why not in the constructor of TitiWidget put this ?

    @
    setStyleSheet(
    "border: 2px solid green;"
    "border-radius: 20px;"
    "padding: 2px;"
    "background-color: rgb(85, 85, 255);"
    );@
    [/quote]

    Because it all depends on how TitiWidget is implemented if this is going to work or not.



  • It only works if you overwrite the paint event.
    Otherwise nothing happens.



  • Thanks a lot for your answers.

    The
    @
    void TitiWidget::paintEvent(QPaintEvent* /p_pEvent/)
    {
    QStyleOption opt;
    opt.init(this);
    QStylePainter p(this);
    p.drawPrimitive(QStyle::PE_Widget, opt);
    }
    @

    trick works just fine. And it's actually in Qt's documentation. Shame on me.

    Edit: please use code tags (@) before and after your code section; Andre



  • Nice trick to get at least the basic style sheet stuff painted correctly, but I still don't see a way to get where you can draw, let alone a way to style what you draw yourself using that style sheet...



  • That does not work at all. If you have a custom drawn widget, it can't be styled. Only if you do some basic style sheet stuff and paint on it later...


Log in to reply