Important: Please read the Qt Code of Conduct -

[Resolved] Dynamically change style of control from loaded QSS file

  • In my application, the stylesheet for the entire application is taken from a resource file "default.qss" 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:

    1. merely calling "SetObjectName("Bad");" doens't change the style from methods... 2) 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?

  • The style will not update itself automatically when the value of the property changes. See "this FAQ":

  • Yep:


    Did the job. Thanks!

    (Although, as having polish ancestry, I object to the wording of the API ;-) )

Log in to reply