Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QRadioButton stylesheet font:bold doesn't work?



  • Hello all,

    First, I apologize if this has been asked/answered before. I am creating a simple application with various widgets, buttons, etc on it and am just playing around with stylesheets. For the most part, everything is working correctly except the font for QRadioButton. I have the following:

    QRadioButton::checked{
    color: rgb(255,255,60);
    font-weight:bold;
    }
    

    The color changes appropriately when the radio button is selected, but the font remains the same. I have even tried adding:

    QRadioButton{
    font-weight: normal;
    }
    

    but that doesn't seem to work either. What am I missing?


  • Lifetime Qt Champion

    Hi,

    What version of Qt are you using ?
    On what platform ?
    Do you have any other stylesheet in your application ?



  • @SGaist

    Hello, I am using version 5.9.5 on Windows. I'm doing it in a clunky way, because I just want to see how things work, so I have a header file with the following:

    static QString RBStyle = QString(
    "QWidget{ " \
    "background-color:  rgb();" \
    "color: rgb();" \
    "}"
    "QRadioButton::checked{" \
    "color: rgb(255,255,60);" \
    "font-weight:bold;" \
    "}"
    );
    

    and then in my .cpp file I put:

    this->setstylesheet(RBStyle);
    

    I realize using a .qss file would be better, but for the purpose of just messing around, I did it this way. Everything else I put seems to work, font just doesn't for whatever reason.



  • @Sh1gs
    https://stackoverflow.com/questions/37802571/how-to-use-qss-to-set-radio-button-bold-when-checked is reporting the same kind of problem as you. Although the OP seems to say the solutions did not work, I think you should read through and just try them!


  • Moderators

    @Sh1gs
    some thoughts from my side:

    1. using a :: in a selector specifies a sub-control of the element, But there is no checked subcontrol. You should rather use :checked. (Even though the official stylesheet docs seem to wrongly mix it up). But i am wondering that it works with the sub-control selector.
    2. Try to unset the font-weight with a QRadioButton:unchecked selector


  • @raven-worx

    By that thinking, then when the radiobutton is selected, the color shouldn't change, but it does. Changing it to : from :: didn't work either. I also tried ::unchecked to set the font weigh to normal and that didn't work :( I don't know what else to try. I've tried font, font-weight, font-style, and nothing seems to change the font to bold

    Ultimately, I'm fiddling with stylesheets for a work project and would like to find a way to make this happen.



  • @Sh1gs
    At this point you should get rid of any :checked etc. in the selector and just try an unconditional

    "QRadioButton {" \
    "font-weight:bold;" \
    "}"
    

    I imagine you will say this does not bold for you, and so we can eliminate anything about the :checked qualifier being of any relevance.

    P.S.
    Give a quick desperation try to font-weight:700;" instead of bold, just in case....


  • Lifetime Qt Champion

    Hi
    It seems the states checked unchecked do not use any font statements included.
    alt text

    They react fine to color change but any font statements seems to be ignored.
    However, its does use its font inherited from QWidget as
    targeting that, font is changed.
    Also using RadioButton alone

    
    QRadioButton {
    font: 750 24pt "Arial";
    }
    
    

    So bascially im not sure its possible to make it go bold on checked using
    stylesheets alone.



  • @mrjj
    That is indeed how it was beginning to look. (Though I see some old web posts where users claim bold does work, some do not, various Qt versions.)

    So you should try font-style: italic, and other individual font- styles, to verify just which style attribute(s) are affected. Before you check through the Qt source code (it's to do with font in QRadioButton, so it can't be hard to find) and raise a bug report depending on what you find :)



  • @JonB

    Using the unconditional, the text changes color AND gets bolded properly.



  • @mrjj

    Reading through the Help files, QPushButton has the ability to customize the font. Both are box models, why do you think QRadioButton doesn't do it?


  • Lifetime Qt Champion

    @Sh1gs
    hi
    well it does respond to font ,
    but not in ::checked ::unchecked
    as far as i could see from testing.
    The states clearly work as the color was changed
    but regardless of font statement used in those
    states, i didn't see any effect even same font statement worked
    with no state syntax.
    So its either a bug, or simply not support for such states.



  • @mrjj , @Sh1gs
    I did reference https://stackoverflow.com/questions/37802571/how-to-use-qss-to-set-radio-button-bold-when-checked above, which is exactly the situation you are reporting.

    The "solution" there is only in a comment:

    At the end, I use a property to indicate the font bold / normal and setProperty in the slot function. It worked! I wonder the difference between property and checked state

    if you can figure what that OP means he did by this and it works for you.


  • Moderators

    @JonB said in QRadioButton stylesheet font:bold doesn't work?:

    if you can figure what that OP means he did by this and it works for you.

    Probably this:

    QRadioButton[checked="true"] {
       font-weight: bold;
    }
    

    additionally to that you could try:

    connect( radioButton, &QRadioButton::toggled, this, [radioButton]() { 
        radioButton->style()->polish(radioButton);
    } );
    


  • @raven-worx
    I do think the OP here should try just that.

    However, the way the OP phrased what he wrote in the stackoverflow comment:

    I use a property to indicate the font bold / normal and setProperty in the slot function.

    might suggest he had to use a dynamic property (other than checked), which he added/removed or switched on/off in the stateChanged slot handler, to "shadow" the :checked state and get it to work with font change....


Log in to reply