Results 1 to 6 of 6

Thread: setting radio button inner circle color to green

  1. #1
    Join Date
    Jan 2021
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Post setting radio button inner circle color to green

    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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: setting radio button inner circle color to green

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2021
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Post Re: setting radio button inner circle color to green

    Qt Code:
    1. QPalette pal= ui->Is_connected->palette();
    2. pal.setColor(QPalette::Button, QColor (Qt::darkGreen));
    3. ui->Is_connected->setPalette(pal);
    4. ui->Is_connected->setEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: setting radio button inner circle color to green

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2021
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: setting radio button inner circle color to green

    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

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: setting radio button inner circle color to green

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Circle color palette
    By binaural in forum Qt Quick
    Replies: 0
    Last Post: 1st February 2011, 22:30
  2. Replies: 3
    Last Post: 26th July 2010, 03:23
  3. Changing Qt's green application back ground color
    By anafor2004 in forum Qt Programming
    Replies: 0
    Last Post: 17th June 2009, 12:58
  4. Replies: 3
    Last Post: 10th April 2009, 15:49
  5. Programatically setting radio buttons
    By Doug Broadwell in forum Newbie
    Replies: 2
    Last Post: 25th May 2007, 21:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.