PDA

View Full Version : changing button background color



Tomasz
25th November 2010, 10:31
Hello!

I've got some buttons on my main window. Each button is setting page on QStackedWidget. I want to change color of a button which was clicked. Is there any other way than using setStyleSheet? I'm asking because my stylesheet is quite complicated, and I want to change just background color. Any ideas?

thanks in advance
best regards
Tomasz

BalaQT
25th November 2010, 11:07
hi,

u can use QPalette


QPalette p=palette();
p.setBrush(QPalette::Button,Qt::yellow);
p.setBrush(QPalette::ButtonText, Qt::blue);
QPushButton *b=new QPushButton;
b->setPalette(p);

hope it helps
bala

axisdj
25th November 2010, 17:58
Third party here:

That will make the button a solid color, is there a way to change the button color with shades, so it looks like a modern button. Like it does by default, except chaning the theme color.

Any suggestions?

SixDegrees
25th November 2010, 18:06
There is no simple way to do this with palettes or stylesheets. On some operating systems it will work, but others (<cough>Windows<cough>) do not allow button backgrounds to be altered. The only foolproof way to do this is to derive your own button class and override the paint method.

BalaQT
27th November 2010, 04:09
Third party here:

That will make the button a solid color, is there a way to change the button color with shades, so it looks like a modern button. Like it does by default, except chaning the theme color.

Any suggestions?

To make the shade in buttons , u can use stylesheets's transparent property or u can design ur own images and apply to the button;

for Gradient:

QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #88d, stop: 0.1 #99e, stop: 0.49 #77c, stop: 0.5 #66b, stop: 1 #77c);

for images :

background-image: url(/path/to/image.png);

Hope it helps

Bala