Stylesheet recomputing after property change



  • I need to change the Style of a QPushButton at execution time. So, what I try to do is using a property.
    I set a property for a QPushButton object of my Custom widget then i would try to recompute the stylesheet but I need to not use setStylesheet call.
    I tried bot unpolish/polish and setStyle.
    None of them seems to work. Yhe only way I can obtain what I want is by using setStyleSheet call.
    Here are the examples of what I did:

    MyClass:Myclass()
    {
         QString strStyleSheet = styleSheet();
         strStyleSheet = "QPushButton#myPushButton[mode=\"medalrm\"]{border-image: 
         url(:/Images/buttonYellow.png) stretch stretch;}";
    }
    
    MyClass:function1()
    {
       ui->myPushButton->setProperty("mode", "medalrm");
       ui->myPushButton->style()->unpolish(ui->myPushButton);
       ui->myPushButton->style()->polish(ui->myPushButton);
       ui->myPushButton->update();
    }
    
    MyClass:function2()
    {
       ui->myPushButton->setProperty("mode", "medalrm");
       ui->myPushButton->setStyle(QApplication::style());
    }
    
    MyClass:function3()
    {
       ui->myPushButton->setProperty("mode", "medalrm");
       ui->myPushButton->setStyleSheet(styleSheet());
    }
    

    Only function3 works properly.
    Any ideas?

    Thank you


  • Moderators

    @SilvioP.
    do the following:

    widget->style()->unpolish(widget);
    widget->style()->polish(widget);
    QEvent event(QEvent::StyleChange);
    QApplication::sendEvent(widget, &event);
    widget->update();
    widget->updateGeometry();
    

    widget->style()->polish(widget) alone only works as long as the style doesn't change the widget's box model.



  • Thank you for the reply raven.
    Actually the property shoud not affect the widget's box model.
    In any case I tried but still nothing change.

    I'm using Qt 4.8.6


  • Qt Champions 2016

    @SilvioP.
    Hi
    Can you test same code in 4.8 ?
    (works in 5.8)
    alt text

    https://www.dropbox.com/s/l7ire2gwa7fy08e/mybutton.zip?dl=0


  • Moderators

    @SilvioP. said in Stylesheet recomputing after property change:

    In any case I tried but still nothing change.

    thats what actually happens when you call setStyleSheet().
    I assume you have only posted fragments of your code?! I think you might have an issue somewhere else?



  • Thx mrjj I tried your code. It works fine. Actually it works even if I use the normal unpolish/polish/update. I don't know what is wrong with my project. I tried even by using an image; same result: everything is as I espect.
    I don't know what can be wrong with my project.


  • Qt Champions 2016

    @SilvioP.
    Can you trigger the style sheet in Designer?
    Maybe it cant load image ?
    And did you call setObjectName on Myclass so its called myPushButton?


Log in to reply
 

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