PDA

View Full Version : Get background color of QPushbutton



MickyJhon
6th February 2014, 15:47
Hi,all.

I have designed some QPushbutton controls using QT designer ,and set background color of the push-buttons to some color. Just like:
10022, Now i only can get Qstring about background color by code:

QString str=ui->btn_curveColor->styleSheet();
cout<<str.toAscii().data()<<endl;.
it gives me :


background-color: rgb(0, 0, 255);

is there a simple way to get background-color in QColor format? I don't want to convert the string to color.

best wishes.

Micky Jhon

anda_skoa
6th February 2014, 17:28
You could try through the button's palette. see QWidget::palette() and QPalette

Cheers,
_

MickyJhon
7th February 2014, 02:24
hi ,anda_skoa :

i use code :


curveColor=ui->btn_curveColor->palette().button().color();
//curveColor=ui->btn_curveColor->palette().color(QPalette::Button); // have the same effect as the above.
int r=curveColor.red();
int g=curveColor.green();
int b=curveColor.blue();
int a=curveColor.alpha();
cout<<"r="<<r<<"\tg="<<g<<"\tb="<<b<<"\ta="<<a<<endl;

however, i only get the output result :


r=236 g=233 b=216 a=255

that's the push-button's default background color, not the result
r=0 g=0 b=255 a=255 as i except. how can i do?

best wishes.

Micky Jhon

You could try through the button's palette. see QWidget::palette() and QPalette

Cheers,
_

anda_skoa
7th February 2014, 08:04
Have you tried the Background or Window roles?

Cheers,
_

MickyJhon
7th February 2014, 12:23
hello, anda_skoa:

According to your opinion ,i use Background or Window roles. however, i don't get the result as i except.

my code is :


QPalette::ColorRole role=ui->btn_curveColor->backgroundRole();
curveColor=ui->btn_curveColor->palette().color(role);
int r=curveColor.red();
int g=curveColor.green();
int b=curveColor.blue();
int a=curveColor.alpha();

QString role1=ui->btn_curveColor->windowRole();

cout<<"Use backgroundRole : r="<<r<<"\tg="<<g<<"\tb="<<b<<"\ta="<<a<<endl;
cout<<"Use windowRole : "<<role1.toAscii().data()<<endl;

I get :

Use backgroundRole : r=236 g=233 b=216 a=255
Use windowRole :

in debug mode. i check the value of role is Button(1), and the role1 is a empty string.

help me please.

Micky Jhon





Have you tried the Background or Window roles?

Cheers,
_

anda_skoa
7th February 2014, 15:10
Seems the style sheet does not change the palette but somehow overrides it.

Too bad, seems you have to parse the stylesheet string yourself.

Cheers,
_

MickyJhon
7th February 2014, 16:45
hi, anda_skoa.thank you all the same. i have to set the flat and AutoFillBackground attributes to true .so i get the result as i except.

code is :

ui->setupUi(this);
ui->btn_curveColor->setFlat(true);
ui->btn_curveColor->setAutoFillBackground(true);
curveColor=ui->btn_curveColor->palette().color(QPalette::Button);

int r=curveColor.red();
int g=curveColor.green();
int b=curveColor.blue();
int a=curveColor.alpha();

cout<<"r="<<r<<"\tg="<<g<<"\tb="<<b<<"\ta="<<a<<endl;

best wishes.

Micky Jhon




Seems the style sheet does not change the palette but somehow overrides it.

Too bad, seems you have to parse the stylesheet string yourself.

Cheers,
_