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

守給你的承諾、 提交于 2020-01-05 03:35:45

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/39929618/pyqt-application-restore-the-original-look-feel-after-changing-some-qss-eleme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!