使用样式表设置 QGroupBox 标题字体大小

问题描述:

我希望能够使用样式表设置 QGroupBox 标题的字体大小.我似乎无法弄清楚.

I would like to be able to set the font size of the title of a QGroupBox using style sheets. I can't seem to figure it out.

根据我阅读的内容 这里,我想出了以下代码.不幸的是,它并不完全有效.

Based on what I've read here, I've come up with the following code. Unfortunately, it doesn't quite work.

groupbox->setStyleSheet(style)

style 在哪里:

QGroupBox::title
{
    subcontrol-origin: margin;
    subcontrol-position: top left;
    padding: 5 5px;
    font-size: 18px;
    font-weight: bold;
}

除了 font-sizefont-weight 之外,所有这些样式元素似乎都受到尊重.根据 Qt 样式表参考,字体所有尊重 QWidget::font 的小部件都支持属性."QGroupBox的标题不是这样吗?

All of those style elements seem to be honored except font-size and font-weight. According to the Qt Style Sheets Reference, the font "property is supported by all widgets that respect the QWidget::font." Is this not the case for a QGroupBox's title?

答案是否定的,QGroupBox的标题不支持QWidget::font 财产.我怀疑标题不是独立的 QWidget 而是 QGroupBox 小部件的一部分(因此由 QGroupBox::paint() 绘制).

The answer is "no", the title of a QGroupBox does not support the QWidget::font property. I suspect that the title is not an independant QWidget but a part of the QGroupBox widget (thus drawn by the QGroupBox::paint()).

但是,GroupBox 小部件支持字体属性,并且由于组框显示的唯一文本是其标题,您可以将字体样式应用于 QGroupBox 小部件.

However, the GroupBox widget supports the font property and since the only text displayed by a group box is its title, you can apply your font style to the QGroupBox widget.

QGroupBox
{
    font-size: 18px;
    font-weight: bold;
}