1 Attachment(s)
Get background color of QPushbutton
Hi,all.
I have designed some QPushbutton controls using QT designer ,and set background color of the push-buttons to some color. Just like:
Attachment 10022, Now i only can get Qstring about background color by code:
Code:
QString str
=ui
->btn_curveColor
->styleSheet
();
cout<<str.toAscii().data()<<endl;
.
it gives me :
Code:
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
Re: Get background color of QPushbutton
You could try through the button's palette. see QWidget::palette() and QPalette
Cheers,
_
Re: Get background color of QPushbutton
hi ,anda_skoa :
i use code :
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 :
Code:
r=236 g=233 b=216 a=255
that's the push-button's default background color, not the result
Code:
r=0 g=0 b=255 a=255
as i except. how can i do?
best wishes.
Micky Jhon
Quote:
Originally Posted by
anda_skoa
You could try through the button's palette. see QWidget::palette() and QPalette
Cheers,
_
Re: Get background color of QPushbutton
Have you tried the Background or Window roles?
Cheers,
_
Re: Get background color of QPushbutton
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 :
Code:
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 :
Code:
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
Quote:
Originally Posted by
anda_skoa
Have you tried the Background or Window roles?
Cheers,
_
Re: Get background color of QPushbutton
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,
_
Re: Get background color of QPushbutton
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 :
Code:
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
Quote:
Originally Posted by
anda_skoa
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,
_