我想要将QGroupBox的标题改为粗体,而其他人则保持不变。如何只更改QGroupBox标题的字体?
发布于 2014-03-06 14:50:34
如果没有显式设置,Font properties将从父继承到子。您可以通过QGroupBox的setFont()方法更改其字体,但是您需要通过显式地重置其子节点上的字体来打破继承。如果您不想对每个单独的子部件(例如,每个QRadioButton)分别设置它,则可以添加一个中间小部件,例如
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);还请注意,当向小部件分配新字体时,“此字体的属性与小部件的默认字体相结合,从而形成小部件的最终字体”。
一种更简单的方法是使用样式表--与CSS不同,也不像普通字体和颜色继承, inherited。
groupBox->setStyleSheet("QGroupBox { font-weight: bold; } ");发布于 2016-08-23 14:38:41
上面的答案是正确的。以下是一些可能有用的额外细节:
1)我在
Set QGroupBox title font size with style sheets
因为QGroupBox::title不支持字体属性,所以不能这样设置标题字体。你需要像上面那样做。
2)我发现setStyleSheet()方法比使用QFont更加“流线型”。也就是说,您还可以执行以下操作:
groupBox->setStyleSheet("font-weight: bold;");
widget->setStyleSheet("font-weight: normal;");发布于 2021-06-08 13:58:32
我从PyQt5而不是Qt直接搜索了这个问题,所以这里是我在python中的答案,希望它能帮助与我一样处境的其他人。
# 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)https://stackoverflow.com/questions/22227376
复制相似问题