【发布时间】:2019-02-24 20:40:38
【问题描述】:
我有一个自定义小部件,其父级是另一个自定义小部件。我可以使用QPalette 设置父自定义小部件的背景颜色,并且效果很好。但我无法同时使用QPalette 和stylesheet 设置子自定义小部件的边框颜色。
这就是我设置父自定义小部件背景颜色的方式:
QPalette pal = parentCustomWidget->palette();
QColor color = {226, 208, 208};
pal.setColor (QPalette::Background, color);
parentCustomWidget->setAutoFillBackground (true);
parentCustomWidget->setPalette (pal);
parentCustomWidget->show();
我参考了几个 SO 帖子/答案来为自定义小部件设置背景颜色,但我无法设置它。这就是我设置childCustomWidget 的颜色的方式:
方法1:
QPalette pal = childCustomWidget->palette();
QColor color = {204, 231, 47};
pal.setColor (QPalette::Background, color);
childCustomWidget->setAutoFillBackground (true);
childCustomWidget->setPalette (pal);
方法二:
childCustomWidget->setStyleSheet ("*{border-width:" +
BorderThickness +
";border-style:solid;border-color:" +
hexColorCode + " ;color:white;}");
注意:我已经注释掉了paintEvent 虚函数。
我已经浏览了这个链接:How to Change the Background Color of QWidget 并合并了给定的更改,但我无法将颜色设置为childCustomWidget。
我使用上述方法的自定义小部件如下所示:
这里橙色是我可以设置的父母的背景颜色。灰色似乎是子小部件的默认颜色。
【问题讨论】:
标签: c++ qt background-color qwidget qtstylesheets