PDA

View Full Version : Change background color of QPushButton



gtthang
10th May 2006, 11:50
Hi all,
I can change the text color of a QPushButton object using the QPalette::ButtonText attribute but I don't know how to change the background color of that button?
I tried the QPalette::Button, QPalette::Background,... but nothing changes.
Help me!
Thanks very much!

wysota
10th May 2006, 12:32
QPalette::Button should have worked. You must have missed something while trying it. If it doesn't, maybe your QStyle doesn't allow changing the background colour of a button (I guess WindowsXP style can do such a thing).

nupul
10th May 2006, 12:55
Hi all,
I can change the text color of a QPushButton object using the QPalette::ButtonText attribute but I don't know how to change the background color of that button?
I tried the QPalette::Button, QPalette::Background,... but nothing changes.
Help me!
Thanks very much!

Well it works fine for me....on WinXP and SuSE 9/10

here's my snippet



QPalette pal=palette(); //get button palette
pal.setBrush(QPalette::Button,Qt::black); //makes my button black
pal.setBrush(QPalette::ButtonText, Qt::white);
mybutton.setPalette(pal);


I presume you are doing the same...I am just confirming it's working! ;)

Nupul

paulo_gomes
26th November 2013, 10:23
I am using the exact same piece of code as mentioned. But only my text colour is changing to black. My button colour does not change. I am using Qt Creator 2.8.1 Based on Qt 5.1.1. Any suggestions ?

1. QPalette pal=palette(); //get button palette
2. pal.setBrush(QPalette::Button,Qt::black); //makes my button black
3. pal.setBrush(QPalette::ButtonText, Qt::white);
4. mybutton.setPalette(pal);