Should I really use a single qss file for my appli

2019-08-10 19:49发布

问题:

Some posts (namely, these two: Set CSS in QPushButton's subclass's constructor and Does the use of styleSheets in a dynamic manner add a lot of computation) suggest that I should use a single qss file for my Qt application.

However, imagine I have a QLabel subclass whose purpose is (using stylesheets) to provide a particular look/behavior (say BlinkingLabel, however horrendous it would look). In that case, doesn't it make sense to prepare a separate stylesheet and load it in the class constructor? Wouldn't using a global file take away a very useful aspect of using OOP for UI components? Because if I do so, I can no longer just take the class and put it in another project without worrying about anything, because it can take care of itself; I have to remember to copy the relevant stylesheet entries.

回答1:

Common answer: it depends...

From my point of view, if you will split your qss to several files and implement a good logic to load them correctly - it will be better solution in compare with single file. As extra option, you still may have a global qss with basic styles.

First reason, why splitting is better: your qss will be more readable and it will me more easy to provide a support.

Second: smaller qss files, applied on different widgets works a bit faster.

P.S. If you need to reload qss on a widget (as described under your links), you need to call polish() + unpolish() instead of resetting qss text.

P.P.S. don't use stylsheets for animations. There is a QPropertyAnimation class. You can use it to animate color, font, etc. on your widget.