我有一个自定义小部件,其父级是另一个自定义小部件。我可以使用设置父自定义小部件的背景颜色QPalette
而且效果很好。但我无法使用两者来设置子自定义小部件的边框颜色QPalette
and stylesheet
.
这就是我设置父自定义小部件背景颜色的方法:
QPalette pal = parentCustomWidget->palette();
QColor color = {226, 208, 208};
pal.setColor (QPalette::Background, color);
parentCustomWidget->setAutoFillBackground (true);
parentCustomWidget->setPalette (pal);
parentCustomWidget->show();
我提到了一些关于将背景颜色设置为自定义小部件的帖子/答案,但我无法设置它。这就是我设置我的childCustomWidget
的颜色:
方法一:
QPalette pal = childCustomWidget->palette();
QColor color = {204, 231, 47};
pal.setColor (QPalette::Background, color);
childCustomWidget->setAutoFillBackground (true);
childCustomWidget->setPalette (pal);
方法2:
childCustomWidget->setStyleSheet ("*{border-width:" +
BorderThickness +
";border-style:solid;border-color:" +
hexColorCode + " ;color:white;}");
Note:我已经注释掉了paintEvent
虚函数。
我已经浏览过这个链接:如何改变QWidget的背景颜色并合并了诸如给定的更改,但我无法将颜色设置为childCustomWidget
.
我使用上述方法的自定义小部件如下所示:
这里橙色是我可以设置的父级的背景颜色。灰色似乎是子窗口小部件的默认颜色。