简体   繁体   English

在另一个小部件内更改QWidget的背景颜色

[英]Changing background color of QWidget inside another Widget

I have a widget that I am placing inside another widget that I wish to make black. 我有一个小部件,将其放置在另一个我希望变黑的小部件中。 I am doing with the following code. 我正在使用以下代码。

Here is my inner widget constructor: 这是我内部的小部件构造函数:

innerwidget.cpp innerwidget.cpp

#include "innerwidget.h"
#include <QStyle>

InnerWidget::InnerWidget(QWidget *parent) : QWidget(parent)
{
    setMinimumSize(1280,70);
    this->setStyleSheet("background-color: black");
    style()->unpolish(this);
    style()->polish(this);
    update();
}

And here is my outer widget constructor: 这是我的外部小部件构造函数:

outerwidget.cpp externalwidget.cpp

#include "outerwidget.h"
#include <QGridLayout>

OuterWidget::OuterWidget(QWidget *parent)
    : QWidget(parent)
{
    setMinimumSize(1280, 800);
    setMaximumSize(1280,800);
    innerWidget = new InnerWidget(this);
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(innerWidget, 0, 0, 0, 4, Qt::AlignTop);
    this->setLayout(layout);
    this->setWindowState(Qt::WindowFullScreen);
}

OuterWidget::~OuterWidget()
{

}

My main is the very simple default code: 我的主要是非常简单的默认代码:

main.cpp main.cpp

#include "outerwidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    OuterWidget w;
    w.show();

    return a.exec();
}

My header files are nothing special: 我的头文件没什么特别的:

innerwidget.h 内部小工具

#ifndef INNERWIDGET_H
#define INNERWIDGET_H

#include <QWidget>

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

signals:

public slots:
};

#endif // INNERWIDGET_H

outerwidget.h externalwidget.h

#ifndef OUTERWIDGET_H
#define OUTERWIDGET_H

#include <QWidget>
#include <innerwidget.h>

class OuterWidget : public QWidget
{
    Q_OBJECT

public:
    OuterWidget(QWidget *parent = 0);
    ~OuterWidget();

private:
    InnerWidget *innerWidget;
};

#endif // OUTERWIDGET_H

I've noticed that if I add controls to the inner panel, such as a QLabel, the background color of those controls will be changed, but I'd like to change the background color of the whole widget (which, in my example, would be a bar across the top of the screen). 我注意到,如果我将控件添加到内部面板(例如QLabel),则这些控件的背景色将被更改,但是我想更改整个小部件的背景色(在我的示例中,将是屏幕顶部的条形)。

Where am I going wrong? 我要去哪里错了? Does it have something to do with the Grid Layout I am using to add my inner widget to outer widget? 它与我用来将内部小部件添加到外部小部件的网格布局有关吗?

Anything which is added to your inner panel will inherit its style sheet, and since you're setting everything to have a background color of black, they will all get this too. 内面板上添加的任何内容都将继承其样式表,并且由于您将所有内容都设置为具有黑色背景色,因此它们也都将获得此样式。

There are many ways to overcome this. 有很多方法可以克服这个问题。 See the official Style Sheet Syntax documentation. 请参阅官方的样式表语法文档。

One option is to use css class selectors. 一种选择是使用CSS类选择器。

InnerPanel::InnerPanel(QWidget *parent) : QWidget(parent)
{
    ...
    setProperty("class", "inner-panel");
    setStyleSheet(".inner-panel { background-color: black }");
}               

Update due to comment: 由于评论而更新:

This actually sounds like a bug in Qt, as a widget's own style is supposed to take precedence over its ancestors'. 实际上,这听起来像Qt中的错误,因为小部件自身的样式应该优先于其祖先的样式。

A workaround would be to set the style of the parent. 一种解决方法是设置父项的样式。

InnerPanel::InnerPanel(QWidget *parent) : QWidget(parent)
{
    ...
    parent->setStyleSheet("background-color: black");
}               

After much experiementing I found an answer to this problem. 经过多次实验后,我找到了解决该问题的方法。

It seems overriding the paitnEvent() method is required to use style sheets with custom widgets, as described here 这似乎重写paitnEvent()方法需要使用样式表自定义窗口小部件,如描述在这里

Note : If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget : 注意 :如果从QWidget继承了自定义窗口小部件,则为了使用StyleSheets,需要向该自定义窗口小部件提供paintEvent:

void CustomWidget::paintEvent(QPaintEvent* event)
{
 QStyleOption opt;
 opt.init(this);
 QPainter p(this);
 style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

 QWidget::paintEvent(event);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM