5
votes

Is there any way to change background color of widget without loosing of it's style? My problem is reproducible on Windows 7/Vista with Aero theme and on Windows 8 too. You can see how it looks in Qt Designer:

Background colour is the only difference between top and bottom buttons

Here we have four buttons: both top widgets looks good (as all other widgets of this program), but both bottom widgets looks old. The only reason of this old style is setting of background color of these buttons (as far as I understand, Desktop Window Manager resets style to old if widget is not standard).

So, is there any way to force Desktop Window Manager to draw button with not standard background color? (Microsoft Office can do it, but I am not sure it is standard feature)

Could you recommend Qt extension which replaces standard drawing of buttons by modern (Aero theme)? Or may be you know more standard way to do it?

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

Workaround (semi-transparent frame is above buttons)

It works, but I don't like this "solution". Do you have better idea?

2
Unfortunately, Windows doesn't natively support that, so whatever solution you will come up with has to be your own. The best you can do is draw the button on an image/pixmap, then colorize it the way you wish.Kuba hasn't forgotten Monica

2 Answers

2
votes

I see that you are using stylesheets to modify the background colour of the buttons. I had faced the same problem a while ago. As soon as you apply a stylesheet, button looses the 3rd effect and you don't see hover effect as well. My take on it was to discard using stylesheets and use QPalette to set the colours. For example,

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

So, is there any way to force Desktop Window Manager to draw button with not standard background color? (Microsoft Office can do it, but I am not sure it is standard feature)

You may write your own QProxyStyle and use WinAPI functions to draw buttons with aero style and custom background color. You may look at Qt sources to see, how it done for aero. But it is very complex task.

Stylesheets are not designed to be used with any animations (like hover animation in aero).

Could you recommend Qt extension which replaces standard drawing of buttons by modern (Aero theme)? Or may be you know more standard way to do it?

There is no standard way. If you want to use stylesheets without such unpredictable issues - you need to use "Fusion" style instead of any native styles. "Fusion" style is good base for QSS customization. See documentation

Other chance - is to use QtQuick instead of widgets.