Chipmuenk Chipmuenk - 2 months ago 26
CSS Question

PyQt application: Restore the original look & feel after changing some QSS elements

I'm changing the look of some elements in my pyQt application using a QSS style sheet. This has the effect of overriding many other properties of those elements as well (as noted in the documentation and many tutorials).

Now, I would like to restore these overridden properties, but I haven't managed to find the "original" style sheets on my system where the default themes are defined. Using the information from these style sheets I could restore the rest of the original look.

Could somebody give me hint where to find these style sheets? Or is there a more elegant way to achieve my goals?

Answer

There are no default stylesheets. All the default styling is done by style plugins.

When a stylesheet is set, it creates an instance of QStyleSheetStyle (which is a subclass of QWindowsStyle) and passes it a reference to the current style. The QStyleSheetStyle will try to apply the QSS rules on top of the current style, but where this is not possible, it will fall back to the QWindowsStyle.

And this is where the problems start, because there is no simple way to determine what a particular QSS rule will do. Sometimes it will have a very precise effect, and other times it will completely clobber the normal styling. In addition, the exact outcome depends on what the current style is. So a QSS rule might look good on your own system, but there's no guarantee it will do so on others.

Using stylesheets is therefore always a compromise. Creating a custom QStyle would give you much more control, but stylesheets will usually be much easier to write and maintain. So it's a trade-off between convenience and precision.

However, there is a third option available. Do nothing. Respect the user's choice of widget styling - or at the very least, don't force them to accept yours.