[Solved] Applying style on derived widget with custom property failes.



  • I've created a MyWidget derived from QWidget with a custom property "selected".
    I've set the stylesheet to
    @
    *[selected="true"]
    {
    border: 5px solid #FFF7BC;
    background-color: #BCF7FF;
    }
    @

    When I check the property in Qt-creator, the style is applied.

    When I create a MyWidget in the constructor of MainWindow and set the property, the style is not applied.
    I've added the same style to the MainWindow, reverted the colors, but this style is also not applied.
    When I create a QWidget in the constructor of MainWindow and set the property, the style is applied.

    @
    DpsMainWindow::DpsMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::DpsMainWindow)
    {
    ui->setupUi(this);

    MyWidget* widget = new MyWidget(this);
    widget->setProperty("selected", true);
    widget->style()->unpolish(widget);
    widget->style()->polish(widget);
    widget->update(); //style is not applied !!

    QWidget* qwidget = new QWidget(this);
    qwidget->setProperty("selected", true); //style is applied !!!
    }
    @

    Using QT 5.0.1 on windows 7 64-bit with Qt-Creator 2.6.2.

    Something is missing I quess, but I don't know what that is.

    project files:
    "DynamicPropStyle.pro":http://pastebin.com/FeLXRHNg
    "dspmainwindow.cpp":http://pastebin.com/YsKzj6MC
    "dspmainwindow.h":http://pastebin.com/R7jZWNzm
    "dspmainwindow.ui":http://pastebin.com/8kaekxJ1
    "mywidget.cpp":http://pastebin.com/8aRgP5Wc
    "mywidget.h":http://pastebin.com/8ReXM8wr
    "mywidget.ui":http://pastebin.com/FgH9Tz45



  • You must reimplement paintEvent for your custom widget. Nothing too drastic just this:

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



  • -You do not need to reimplement the paintEvent for your custom widget.-

    -However, you do need to realize that unfortunately, style renderings are not automatically updated on the change of custom properties. To make that work, you will need to unpolish and then repolish your widget, or re-set the style sheet on it. Otherwise, the style sheet is not re-evaluated, and your property change has no effect. I have filed a bug on that a long time ago already...-



  • [quote author="Andre" date="1363171690"]You do not need to reimplement the paintEvent for your custom widget.

    [/quote]

    But in "Qt Style Sheets Reference":http://qt-project.org/doc/qt-4.8/stylesheet-reference.html document it is specified that

    bq. QWidget
    Supports only the background, background-clip and background-origin properties.
    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);
    } @

    bq. The above code is a no-operation if there is no stylesheet set.

    So for a custom widget should we reimplement the painEvent or not ?



  • Actually, I think I might be wrong. Sam gave a detailed explanation why. Thanks for correcting me.



  • @Andre

    QWidgets + StyleSheets is always confusing to me thats why I added a note in "How to Change the Background Color of QWidget":http://qt-project.org/wiki/How_to_Change_the_Background_Color_of_QWidget and "Trigger an update for the widget while using dynamic properties.":http://qt-project.org/doc/qt-5.0/qtwidgets/stylesheet-syntax.html#note-238.



  • Great, it works.

    I've added the paintEvent and I've created a setter for this property where I update the style:

    @
    void MyWidget::setSelected(bool value)
    {
    setProperty("selected", value);
    style()->unpolish(this);
    style()->polish(this);
    update();
    }
    @



  • Can someone explain why painEvent needs to be reimplemented? This seems very unintuitive when all that's needed is the default stylesheet functionality.


Log in to reply
 

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