如何设置自定义窗口小部件的背景颜色和边框宽度? [英] How to set a custom widget's background color and border width?

查看:655
本文介绍了如何设置自定义窗口小部件的背景颜色和边框宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义窗口小部件,其父级是另一个自定义窗口小部件.我可以使用QPalette设置父自定义窗口小部件的背景颜色,并且效果很好.但是我无法同时使用QPalettestylesheet设置子级自定义小部件的边框颜色.

I have a custom widget whose parent is yet another custom widget. I am able to set the background color of the parent custom widget using QPalette and it works fine. But I am unable to set the child custom widget's border color using both QPalette and stylesheet.

这是我设置父自定义窗口小部件的背景颜色的方式:

This is how I set my parent custom widget's background color:

QPalette pal = parentCustomWidget->palette();
QColor color = {226, 208, 208};
pal.setColor (QPalette::Background, color);
parentCustomWidget->setAutoFillBackground (true);
parentCustomWidget->setPalette (pal);
parentCustomWidget->show();

我提到了几个SO帖子/答案,用于将背景色设置为自定义小部件,但我无法对其进行设置.这就是我设置childCustomWidget颜色的方法:

I referred several SO posts/answers for setting background color to custom widget, but I am unable to set it. This is how I set my childCustomWidget's color:

方法1:

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;}");

注意:我已经注释掉了paintEvent虚拟功能.

Note: I have commented out the paintEvent virtual function.

我已经通过以下链接访问:如何更改QWidget的背景颜色,并进行了更改就像给定的一样,但我无法将颜色设置为childCustomWidget.

I have gone through this link: How to Change the Background Color of QWidget and have incorporated changes like given but im unable to set color to childCustomWidget.

使用上述方法的我的自定义小部件如下:

My custom widgets with the above approaches look like this:

橙色是我可以设置的父母的BG颜色.灰色似乎是子窗口小部件的默认颜色.

Here orange is the parent's BG color which I am able to set. The grey colored one seems to be the default color for the child widget.

推荐答案

解决方案

要使 Approach2 起作用,即为了使您的自定义窗口小部件遵守样式表,请

Solution

For Approach2 to work, i.e. for your custom widget to respect the stylesheet, the Qt::WA_StyledBackground attribute should be set to true, as it:

表明该小部件应使用样式化的背景绘制.

Indicates the widget should be drawn using a styled background.

示例

这是我为您准备的一个最小示例,目的是演示建议的解决方案的可能实现:

Example

Here is a minimal example I have prepared for you in order to demonstrate a possible implementation of the suggested solution:

class ParentWidget : public QWidget
{
    Q_OBJECT
public:
    explicit ParentWidget(QWidget *parent = nullptr) : QWidget(parent) {}
};

class ChildWidget : public QWidget
{
    Q_OBJECT
public:
    explicit ChildWidget(QWidget *parent = nullptr) : QWidget(parent) {}
};

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0) :
           QMainWindow(parent)
       {
           auto *pWidget = new ParentWidget(this);
           auto *l = new QVBoxLayout(pWidget);
           auto *cWidget = new ChildWidget(pWidget);
           QString BorderThickness("2");
           QString hexColorCode("#FF00FF");

           l->addWidget(cWidget);
           l->setContentsMargins(25, 25, 25, 25);

           QPalette pal(pWidget->palette());
           QColor color(226, 208, 208);
           pal.setColor (QPalette::Background, color);
           pWidget->setAutoFillBackground (true);
           pWidget->setPalette (pal);

           cWidget->setAttribute(Qt::WA_StyledBackground, true);
           cWidget->setStyleSheet("ChildWidget { border: " + BorderThickness + " solid " +
                                  hexColorCode + ";"
                                                 "background-color: rgb(204, 231, 47);"
                                                 "color: white; }");

           setCentralWidget(pWidget);
           resize (400, 400);
       }
};

结果

在撰写本文时,此示例将产生以下结果:

Result

As it is written, this example produces the following result:

这篇关于如何设置自定义窗口小部件的背景颜色和边框宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆