Updating ObjectName of widget does not not update the style automatically
-
20-04-2021 - |
Pregunta
In my application, the stylesheet for the entire application is taken from a resource file loaded into the application.
I have two stylesheets defined there for a QListWidget:
QListWidget#Good{
..some color properties
}
QListWidget#Bad {
..soem different color properties
}
In a form, my QListWidget's Object name is "Good", so it adheres to the #good style. During runtime, I have a case where I want to "SetObjectName" it to "Bad" and have the style changed instantly in the UI.
I have noticed two things:
- merely calling "SetObjectName("Bad");" doens't change the style from methods...
- The only time "SetObjectName("Bad");" works is if called from the constructor of the Widget containing the QListWidget.
Obviously i am missing some "update style" phase. Note that I cannot have "setStyleSheet" in code, it must come from the QSS file.
What am I missing?
Solución
Got this resolved. Calling:
style()->unpolish(theWidget);
style()->polish(theWidget);
On my widget after the change of objectName (setObjectName
) did the trick.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow