PDA

View Full Version : setting radio button inner circle color to green



anita.niharika@gmail.com
1st February 2021, 14:12
Hi All,

How to set color of the radio button to green.

device state : connected - set radio button to green color

I tried ui->radio_button->setStyleSheet("background-color: green");

Can someone help me to fix it?

Thanks,
Anita

d_stranz
1st February 2021, 17:46
Widgets that are 'controls', like radio buttons, are drawn as a set of 'primitive elements'. The Primitive element for the indicator (the circle) is PE_IndicatorRadioButton. If you dig deep into the Qt style source code for the basic style, the outline is drawn using settings from the QPalette for the control.

The QPen is set using the palette's QPalette::dark() color, and this pen draws the outline of the circle.

If the radio button is on (selected, checked), the QBrush is set using the palette's QPalette::windowText() color. This fills the circle. If it is not filled, then no brush is used.

However, be aware that the QPalette::WindowText color is also used to draw the radio button's text, so if you set this palette entry to green, you'll also get green text. The only way to change this is to write your own QStyle.

So you can manipulate the QPalette directly for your radio buttons.

anita.niharika@gmail.com
1st February 2021, 18:31
QPalette pal= ui->Is_connected->palette();
pal.setColor(QPalette::Button, QColor (Qt::darkGreen));
ui->Is_connected->setPalette(pal);
ui->Is_connected->setEnabled(true);


I tried using above piece of code to set the Radio button color. Looks like, it's not working.
You mentioned that to manipulate the QPalette directly for the radio buttons. Could you please elaborate on it.

Best Regads,
Anita

d_stranz
1st February 2021, 19:00
If the radio button is on (selected, checked), the QBrush is set using the palette's QPalette::windowText() color.

Read the answer: WindowText, not Button. And if you are using a custom style, like Fusion or one of the others, then that style might use its own colors.

anita.niharika@gmail.com
1st February 2021, 19:16
Hi,

Thank you for the reply.

Currently I am using below version for my development open source. As I am completely new, not aware of different styles.

Qt Creator 4.14.0
Qt 5.15.2 (MSVC 2019, 64 bit)

My requirement is Based on serial command response to the device connect request, the radio button color should be displayed in green color.
I am no where finding solution for this. Any further approach?

Thanks,
Anita

d_stranz
1st February 2021, 22:30
My requirement

It doesn't matter what your requirement is. If you use the standard QRadioButton, you cannot change the color of the indicator to green without also changing the text color.

As I said above, the code for drawing the parts of the QRadioButton widget is very deep inside the Qt widgets implementation and cannot be customized unless you derive your own radio button and draw it yourself, you modify the QPalette::WindowText property for the button's QPalette, or you implement a custom QStyle to change the way the elements of the QRadioButton are drawn.

That's it. There is no magic solution to be found by searching. What you want to do cannot be done except through a lot of heavy work and a very good understanding of how Qt styles and widgets work together to create what you see on screen.