How to save Windows Aero theme after setting button background color in Qt?

Is there a way to change the background color of a widget without losing it? My problem reproduces on Windows 7 / Vista with the Aero theme and on Windows 8. You can see how it looks in Qt Designer:

Background color is the only difference between the top and bottom buttons

Here we have four buttons: both widgets look good (like all other widgets in this program), but both lower widgets look old. The only reason for this old style is to set the background color of these buttons (as I understand it, the Desktop Window Manager resets the style to the old one if the widget is not standard).

So, is there a way to get the Desktop Window Manager to draw a button with a custom background color? (Microsoft Office can do this, but I'm not sure if this is a standard feature)

Could you recommend the Qt extension, which replaces the standard button pattern with modern ones (Aero theme)? Or maybe you know a more standard way to do this?

I found only one way to change color without losing style: add a partially transparent widget above the button (and make it transparent for mouse clicks). You can see the result here (my frame is larger than necessary - this is for demonstration, I also need color correction to compensate for transparency, but this is not critical):

Workaround (translucent frame above buttons)

This works, but I do not like this "solution". Do you have an idea?

+5
source share
2 answers

I see that you use style sheets to change the background color of the buttons. Some time ago I ran into the same problem. As soon as you apply the stylesheet, the button loses the 3rd effect, and you also do not see the hover effect. My desire was to abandon the use of style sheets and use QPalette to set colors. For instance,

 QPalette bluePalette(button->palette()); // original palette bluePalette.setColor(QPalette::Button, QColor(Qt::blue)); //colour you want button->setPalette(bluePalette); 
+2
source

So, is there a way to get the Desktop Window Manager to draw a button with a non-standard background color? (Microsoft Office can do this, but I'm not sure if this is a standard feature)

You can write your own QProxyStyle and use the WinAPI functions to draw buttons using the aero style and custom background color. You can look at Qt sources to find out how this is done for aero. But this is a very difficult task.

Style sheets are not intended for use with any animations (e.g. hover animation in aero).

Could you recommend the Qt extension, which replaces the standard button drawing with a modern one (Aero theme)? Or maybe you know a more standard way to do this?

There is no standard way. If you want to use style sheets without such unpredictable problems, you need to use the "Fusion" style instead of any native styles. The Fusion style is a good base for customizing QSS. See documentation

Another chance is to use QtQuick instead of widgets.

+1
source

All Articles