Are Qt's stylesheets really handling _dynamic_ properties?
Asked Answered
S

4

17

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

Scarab answered 20/10, 2009 at 15:28 Comment(1)
Maybe you need it to be a Q_PROPERTY ?Scribble
S
6

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.

Scarab answered 20/10, 2009 at 16:4 Comment(4)
This did not work for me with PyQt 4.7.2 (Qt 4.6.2) Ton van den Heuvel's suggestion did though.Sarchet
This works for me! I'm running Qt 4.7 and changes to the "enabled" property via QWidget::setEnabled(bool) weren't changing the look of the widget via the corresponding [enabled="false"] and [enabled="true"] sections in the stylesheet. Using the above worked like a charm. I just wrapped them in a rePolish(QWidget*) function and peppered calls to that function throughout my code wherever the enabled state changes.Ad
As a side-note: Whoever downvoted the above answer... "It doesn't work for me" generally isn't a good enough reason to downvote.Ad
myWidget->ensurePolished() may not apply styles correctly, use style()->polish(theWidget) insteadMidcourse
E
20

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());
Ergot answered 29/9, 2010 at 13:51 Comment(0)
S
6

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.

Scarab answered 20/10, 2009 at 16:4 Comment(4)
This did not work for me with PyQt 4.7.2 (Qt 4.6.2) Ton van den Heuvel's suggestion did though.Sarchet
This works for me! I'm running Qt 4.7 and changes to the "enabled" property via QWidget::setEnabled(bool) weren't changing the look of the widget via the corresponding [enabled="false"] and [enabled="true"] sections in the stylesheet. Using the above worked like a charm. I just wrapped them in a rePolish(QWidget*) function and peppered calls to that function throughout my code wherever the enabled state changes.Ad
As a side-note: Whoever downvoted the above answer... "It doesn't work for me" generally isn't a good enough reason to downvote.Ad
myWidget->ensurePolished() may not apply styles correctly, use style()->polish(theWidget) insteadMidcourse
S
3

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.

Stood answered 4/12, 2009 at 19:28 Comment(1)
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.Sarchet
B
2

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.

Breach answered 20/10, 2009 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.