Important: Please read the Qt Code of Conduct -

[Solved] Appropriate method of using Qt Style Sheets?

  • Hi,
    In my application I want to provide some customisation for the user. I am giving option to change color of some widgets like QLabel, QLineEdit, QGroupBox, QFrame. After searching the Internet I have come to the conclusion that Style Sheets are better than QPalette. So I had a QFrame in my application in which there were other widgets. In Designer module when I changed the background color of the QFrame then it changed that for all its child Widgets. Plus the default way the widgets look in Windows 7, the rounded bevelled corners for Push Button were made into sharp flat corners & it lost the sort of 3D effect of pushing down. So I would like to know the appropriate method of using Style sheets in Designer & also by code.
    PS: I just want to change the color of my widgets, I dont way to change their "feel".
    I hope I could make myself clear...
    Thank You.

  • Moderators

    The "feel" is OS specific. By setting any css style on a widget you are basically turning off the OS drawing of the controls and saying "I will do it myself". This is not a general rule though. For example setting a text or background color on some controls will not turn off the OS styling.
    But usually when you start to style controls yourself you should go the whole mile - style border, background, text etc. and also different states of a control like :selected, :hover etc.

    You might ask why is it so. Well, it's just how native controls work. There usually is no OS api to style a particular single color of eg. animated bloom effect on a button. There are apis like "draw control X in state Y". You can change these colors by adjusting system theme settings, but you can't apply them to selected controls using the OS apis.

    There are some exceptions as I said, because sometimes some particular OS might be generous enough to provide this customization for selected controls, but that is not the general rule.
    Usually it's a "all or nothing" type of situation.

    You might take a look at the "new Fusion style in Qt5": It's not exactly native looking, but quite customizable using QPalette.

  • Pretty Disappointed by Qt I am now...
    Thanks for your detailed reply, I got to know the limitations. Now I will not support customization of color in my software as it is a big headache to configure every aspect of widgets manually. I can understand that the limitation is due to the OS & not Qt itself.
    Regarding Fusion Style I liked it pretty much & would like to appreciate the efforts of people behind all this hard work. But can I point out some flaws/reasons as to why I would still prefer native windows 7 style than Fusion style (please dont read further if you would not like to read some criticism ...):

    1. Fusion style looks dull (Sorry about that :( ). It doesnt have that glossy effect as as in Windows 7 (from what I can make out from the images posted in comments). Just look at the pair of images posted by Morten Johan Sørvig in a comment for Mac OSX.
    2. Radio Button's, PushButton's look 'Aswesome' in W7 but in Fusion they appear just plain 2D dots & blocks. (Sorry for the criticism but I want to improve the quality of Qt!)

    I may not be in a position to comment on the hard work put up by all those people out there just from a few images, but I am just giving you an honest opinion so that Qt becomes the best application development kit.
    Please please dont take it in a bad manner & try to make the widgets more "lively", "beautiful", "fancy" & "loveable to play with (seriously speaking I had created an application with a window & just 1 push button in the centre & I used to press over & over again to observe the color effects whenever I was feeling sad, just made me feel so better, but I dont think I will do that with Fusion style push button)".

  • Moderators

    Fusion style was not really designed for mac or windows. Yes it works, but it's not the primary target. Those OSes have their native styles and these are the default.

    Fusion is more of a "common denominator" style. Doesn't have any distinctive qualities of any OS and thus looks reasonably well everywhere.

    As for Windows 7 - I know it's a pain but what is there to do? Windows theming system changes practically every (or every other) release. Windows XP had Luna, Vista & 7 Aero, Windows 8 drops all of this and sort of gets back to "colored rectangles".
    I'm not even starting on what's going on in Linux world or all other platforms Qt supports.
    It's next to impossible to create a universal style for all of those, and Fusion in my opinion is just as neutral as it can be. Doesn't have all the bells and whistles of all OSes but it's not ugly either.

  • I appreciate the idea of Qt developers of creating a neutral style which looks the same irrespective of the platform. & I am certainly not calling Fusion style "ugly"!. Its just too simple! If you want people to use your style in their applications, then dont you think that you should make them look more better?
    I am not saying make it look like Windows7 or Mac. I am just saying make it more "beautiful". I dont think anybody would like to develop commercial grade applications with Fusion Style as it currently is. I am just saying add some life to it, it is currently very dull.
    Regarding Microsoft philosophy for Windows themes, Yes I agree that they just keep changing their clothes once too often. So the objective of Fusion being a neutral style perfectly fits the bill from your point of view. But think from our point of view. Would you choose a style that looks just the same dull on all platforms or platform specific styles which have their own advatages/disadvantages to which the end user is habitual to.

  • Moderators

    No, but nobody is saying you should choose Fusion. That's why Qt has "native" styles for Windows and Mac. I just mentioned Fusion as an option in aiding your particular problem.
    Fusion can be used where there is no native style or when you wan't to have more control over what your app looks like (you can do pretty crazy stuff with brushes in the palette, not just solid colors).
    Have you seen Ubuntu for example? Fusion fits there perfectly in my opinion.

    Anyway, cause we're starting to spin in circles, native styles should be used if available, otherwise use Fusion or make a custom theme via css(limited when it comes to animated effects) or a style plugin.
    Take a look at what other "giants" do with their apps - Photoshop or even Microsoft's own Visual Studio look anything but native.

    I know it's a lot of work, but maybe it's a good idea then to invest some time in preparing your own style if you don't like what Qt has to offer by default. You could even start with Fusion and make it just right for your needs. Who knows, maybe they'll even include it in Qt 5.x if you contribute :)

  • So you are saying that I can add all those glossy gradients & 3D effects to Fusion style via QPalette?
    If it is true then I take back everything that I have said & just apologise for my ignorance. It would be great if that would be possible to edit the Fusion style in that manner.
    I am just a beginner to Qt so dont know how to do that.
    Would be extremely interested in learning to do that.
    Thank You for all your replies.

  • Moderators

    Well no, not really. To do animations and more advanced stuff you would have to create a QStyle based class and paint those manually either via pixmaps, any OS facilities available or other ways.

    What I meant was that you can set any QBrush for palette roles like background etc. and a brush can be a gradient, pattern or a pixmap, so it's pretty configurable. You won't get any animations with that but nevertheless some nice effects can be made.

Log in to reply