Unsolved 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
-
@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
-
@SilvioP.
Hi
Can you test same code in 4.8 ?
(works in 5.8)
-
@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. -
@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? -
@raven-worx said in Stylesheet recomputing after property change:
QEvent event(QEvent::StyleChange);
QApplication::sendEvent(widget, &event);
widget->update();Thanks! It helped me out too. My dynamic property styling on my QTreeView did not seem to be applied when the property change occurred without user interaction by clicking or key navigation (or even a simple mouse move nudge). In my case, the transition from PLAY to STOP state when a file finishes playing does not require user interaction, so the formatting for the STOP state was not applied. Your solution solved this! Thanks.