tags:

views:

862

answers:

4

Is Qt's dynamic properties really so dynamic with stylesheets?

I have the basic example from stylesheets and dynamic properties:

/*stylesheet:*/
*[field_mandatory="true"] { background-color: "yellow";}

And I have this happening at runtime somewhere in the code:

/*code:*/
myWidget->setProperty("field_mandatory", field->isFilled() );

Nothing changes in UI, when this property is changed at runtime.

Does anyone have ideas what must be done to update Qt's stylesheet engine when changing properties, or is it even capable handling these kinds of cases?

Btw. I'm using Qt 4.4

A: 

I found a quick, although a bit hackish, way to update widget's styling.

myWidget->style()->unpolish(myWidget);
myWidget->ensurePolished();

Doing this after changing properties keeps correlation between property data and UI.

mdcl
This did not work for me with PyQt 4.7.2 (Qt 4.6.2)Ton van den Heuvel's suggestion did though.
eric.frederich
+1  A: 

I tried this too with no luck, and when I found the following text in the documentation, I gave up. See The Style Sheet Syntax:

Warning: If the value of the Qt property changes after the style sheet has been set, it might be necessary to force a style sheet recomputation. One way to achieve this is to unset the style sheet and set it again.

swongu
+1  A: 

The following works for me to ensure a proper restyling of the widget:

myWidget->setStyle(QApplication::style())

Forcing a recomputation of the stylesheets as proposed by mdcl did not work for me. I am using Qt 4.5.3.

Ton van den Heuvel
This did work for me using PyQt 4.7.2 (Qt 4.6.2).A real shame that this stuff doesn't work so nice.You should be able to just call ensurePolished() and be done with it.
eric.frederich
A: 

Qt has the following recommendation in their FAQ:

style()->unpolish(theWidget);
style()->polish(theWidget);

They also say you can reset the stylesheet by doing the following but it is more expensive:

setStyleSheet(styleSheet());
Jason