Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Say No to Qt Style Sheets

Making the Right Choice Between QStyle and Qt Style Sheets, Upfront

ScenarioQt Style SheetsQStyle
Calling setFont/setPaletteNo effect (rather unexpected), even if the style sheet has nothing to do with colorsWorks as usual
Changing the system colorsNo effect — the styled widgets don’t adapt to the new colors, even if the stylesheet has nothing to do with colorsWorks as usual — the widgets use the new colors via the updated QPalette
Achieving 100% of the styling requirementsStyle sheets allow you to do a number of things, but not everything. If there’s no support for a specific customization, there’s no solution.QStyle subclasses in C++ give full control, as long as the widget actually delegates to the style. (Note that style sheets are implemented as an internal QStyle subclass. So, by definition, the flexibility given by custom QStyle subclasses is greater or equal.)
Mixing custom styling with native stylingStylesheets do not mix well with QProxyStyles or other QStyle subclasses. This makes it even harder to gradually move away from the stylesheet solution. One specific example for proof: a stylesheet that simply sets a background color will lead to many of the style’s methods not being called because the style sheet style does many things on its own (ex: CT_SpinBox).QProxyStyle allows you to tweak just one aspect and leave the rest to the native style for the platform.
Making a specific window unstyled (e.g.,file dialog)Very complicated (impacts all CSS rules)Simply call QWidget::setStyle on other windows and not on that one.
Scaling for complex needsActual management of the stylesheet data does not scale (no include mechanism, etc.), calling for super-complicated selectors.The C++ code of the QStyle subclass can call into helper classes, one per type of widget. This is the way KDAB develops widget styles these days, to avoid writing a 20000-line class.
Reacting on changesSelectors on properties do not get reevaluated when the properties change, forcing hacks such as reset of a style sheet with the associated cost.The setter calls update(); the paint event calls into the style again.
Performance considerations when styling many independent widgetsEach call to setStyleSheet triggers parsing, which can be slow if done too often (e.g., when creating 50 buttons, each one calling setStylesheet(“…”)). The usual solution is a single application-wide stylesheet, but that’s a pain to maintain and it hits the issue of management of the style sheet data, above.No parsing necessary. Either all widgets use the application style, or individual widgets can point to a specific style.
Performance considerations whenreparenting a widgetEach reparenting triggers clearing the stylesheet rules cache and recalculating everything.Reparenting has no performance impact with QStyle.

About KDAB


14 Comments

28 - Apr - 2022

Julien Maille

28 - Apr - 2022

David Faure

29 - Apr - 2022

Uwe Kindler

5 - May - 2022

Hamish M

23 - May - 2022

David Faure

15 - Oct - 2022

a_qt_developer

17 - Aug - 2022

David Faure

20 - May - 2022

Donna W

23 - May - 2022

David Faure

8 - Aug - 2023

Anon

12 - Aug - 2022

Alex Unknown

30 - Aug - 2022

David Faure

19 - Mar - 2023

Ryan

21 - Feb - 2024

Phil

DavidFaure

David Faure

Senior Software Engineer

Learn Modern C++

Learn more