2014-03-06 310 views

回答

12

Font properties如果未明确设置,则从父代继承到子代。您可以通过setFont()方法更改QGroupBox的字体,但是您需要通过明确重置其子级的字体来中断继承。如果您不想为每个孩子(例如,每个孩子上分别设置)设置此选项,则可以添加一个中间控件,例如,像

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,不像正常的字体和颜色继承,properties from style sheets are not inherited

groupBox->setStyleSheet("QGroupBox { font-weight: bold; } "); 
+0

此方法是否也适用于对话标题?谢谢。 – user1899020

+0

非常有启发性的答案。非常感谢!!!我认为这种方式也适用于对话标题。大!!! – user1899020

+0

我刚刚尝试过它,并没有马上工作,所以我不确定是否可以完成 - 至少如果它是顶级窗口/对话框,那么本机窗口系统可能会阻止修改字体。可能这有助于:http://www.qtcentre.org/threads/25974-stylesheet-to-QDialog-Title-Bar –

0

以上回答是正确的。 这里有几个额外的细节可能会有所帮助:

1)我在

Set QGroupBox title font size with style sheets

了解到QGroupBox::title不支持字体属性,所以你不能设置标题的字体办法。你需要像上面那样做。

2)我发现setStyleSheet()方法比使用QFont更“精简”一点。也就是说,您还可以执行以下操作:

groupBox->setStyleSheet("font-weight: bold;"); 
widget->setStyleSheet("font-weight: normal;"); 
相关问题