更改QGroupBox的内容背景

时间:2017-03-20 23:19:56

标签: c++ qt c++11 qt5 qt5.5

我想更改QGroupBox的背景,但我想只更改内部背景(每个QGroupBox标题下面的深灰色阴影),如下所示: enter image description here 我现在拥有的是

QGroupBox {
    background-color: red;
    border: 3px dashed black;
}

它会改变整个QGroupBox的背景,如下所示: enter image description here

Qt中是否有办法只更改内部框"背景而不是整个容器?提前谢谢。

3 个答案:

答案 0 :(得分:1)

你需要告诉Qt更多关于你想要的风格,特别是边距。使用此代码稍微玩一下可以获得所需的结果:

QGroupBox {
    background-color: red;
    border: 3px dashed black;
    margin-top: 1ex; /* leave space at the top for the title */
}

QGroupBox::title {
    subcontrol-origin: margin;
    padding: 0 3px;
}

查看Stylesheet examples

答案 1 :(得分:1)

我想这里有2个QGroupBox,因为你的帖子并不是很清楚。或者是否有一个组框和一些其他内部容器小部件?

在任何一种情况下,您都应该能够使用以下样式表:

QGroupBox {
    background-color: red;
    margin-top:1em;
}
QGroupBox QGroupBox {
    background-color: green;
}
QGroupBox::title {
    subcontrol-origin: padding;
    subcontrol-position: left top;
    background: transparent;
    margin-top: -2.5em;
}

这将为您提供以下结果:

Example

您当然可以用任意小部件替换内部组框。

答案 2 :(得分:0)

你可以使用" setStyleSheet"小工具的功能。

获取内部组框对象。并使用" setStyleSheet"设置背景颜色;功能

伪代码:

QGroupBox *innerGBox = new QGroupBox();

innerGBox->setStyleSheet("background-color: red");

要了解有关设置样式编程的更多信息,请参阅以下链接。

http://doc.qt.io/qt-4.8/stylesheet-examples.html