14

I want to change the QGroupBox's title to be bold and others keep unchanged. How to change the font of a QGroupBox's title only?

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
user1899020
  • 13,167
  • 21
  • 79
  • 154

4 Answers4

19

Font properties are inherited from parent to child, if not explicitly set. You can change the font of the QGroupBox through its setFont() method, but you then need to break the inheritance by explicitly resetting the font on its children. If you do not want to set this on each individual child (e.g. on each QRadioButton) separately, you can add an intermediate widget, e.g. something like

QGroupBox *groupBox = new QGroupBox("Bold title", parent);

// set new title font
QFont font;
font.setBold(true);
groupBox->setFont(font);

// intermediate widget to break font inheritance
QVBoxLayout* verticalLayout = new QVBoxLayout(groupBox);
QWidget* widget = new QWidget(groupBox);
QFont oldFont;
oldFont.setBold(false);
widget->setFont(oldFont);

// add the child components to the intermediate widget, using the original font
QVBoxLayout* verticalLayout_2 = new QVBoxLayout(widget);

QRadioButton *radioButton = new QRadioButton("Radio 1", widget);
verticalLayout_2->addWidget(radioButton);

QRadioButton *radioButton_2 = new QRadioButton("Radio 2", widget);
verticalLayout_2->addWidget(radioButton_2);

verticalLayout->addWidget(widget);

Note also, when assigning a new font to a widget, "the properties from this font are combined with the widget's default font to form the widget's final font".


An even easier approach is to use style sheets - unlike with CSS, and unlike the normal font and color inheritance, properties from style sheets are not inherited:

groupBox->setStyleSheet("QGroupBox { font-weight: bold; } ");
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Does this method also work for dialog title? Thanks. – user1899020 Mar 06 '14 at 15:04
  • Very instructive answers. Thanks a lot!!! I think this way also work for dialog title. Great!!! – user1899020 Mar 06 '14 at 15:12
  • 1
    I just tried it, and it did not work right away, so I am not sure if it can be done - at least if it is a top-level window/dialog then the native Window system might prevent modifying the font. Probably this helps: http://www.qtcentre.org/threads/25974-stylesheet-to-QDialog-Title-Bar – Andreas Fester Mar 06 '14 at 15:14
  • Bravo for the stylesheet not being inherited. Life-saver. – quimnuss Sep 03 '15 at 11:17
2

The answer above is correct. Here are a couple extra details which may be helpful:

1) I learned in

Set QGroupBox title font size with style sheets

that the QGroupBox::title doesn't support font properties, so you CAN'T set the title font that way. You need to do it as above.

2) I find the setStyleSheet() method to be a bit more "streamlined" than using QFont. That is, you can also do the following:

groupBox->setStyleSheet("font-weight: bold;");
widget->setStyleSheet("font-weight: normal;");
Community
  • 1
  • 1
dunedin15
  • 3,477
  • 1
  • 12
  • 4
2

I searched for this question coming from PyQt5 not Qt directly, so here is my answer in python hoping that it will help others in the same situation as me.

# Set the QGroupBox's font to bold. Unfortunately it transfers to children widgets.
font = group_box.font()
font.setBold(True)
group_box.setFont(font)  

# Restore the font of each children to regular.
font.setBold(False)
for child in group_box.children():
    child.setFont(font)
Guimoute
  • 4,407
  • 3
  • 12
  • 28
0

At least with Qt 4.8, setting the font to 'bold' with style sheets didn't work for me.

A somewhat simpler version to set all children widgets to normal font which works also when you are using the Qt designer (ui files):

QFont fntBold = font();
fntBold.setBold( true );

ui->m_pGroup1->setFont( fntBold );

auto lstWidgets = ui->m_pGroup1->findChildren< QWidget* >();

for( QWidget* pWidget : lstWidgets )
    pWidget->setFont( font() );
Bernhard
  • 51
  • 3