Set background of QMainWindow central widget

a 夏天 提交于 2019-12-10 15:57:40

问题


Using Qt 4.8.4 on Windows 7 (MSVC 2010) I have a standard QMainWindow in my app with a toolbar. I want the toolbar to stay grey, but the central widget should have a white background. Calling centralWidget->setStyleSheet("background-color: white;") at first seemed to do the job, but using it with a Designer-generated widget (a Q_OBJECT) doesn't. Subsequently I toyed around with various other methods to set a style sheet (also using Designer) to no avail.

To see this effect, add or remove the Q_OBJECT line in test.h. When it's there, only the label gets a white bg. If Q_OBJECT is commented out, the whole central widget is white. Of course, I want the whole area white, but also need Q_OBJECT.

Here's the files:

main.cpp:

#include "test.h"

class testwin : public QMainWindow {
public:
    QWidget     *centralWidget;
    QToolBar    *mainToolBar;

    testwin(QWidget *parent = 0) : QMainWindow(parent) {
        centralWidget = new test(this);
        setCentralWidget(centralWidget);
        mainToolBar = new QToolBar(this);
        this->addToolBar(Qt::TopToolBarArea, mainToolBar);
    };

    ~testwin() {};
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    testwin w;
    w.centralWidget->setStyleSheet("background-color: white;");
    w.show();
    return a.exec();
}

test.h:

#include <QtGui>

class test : public QWidget
{
    Q_OBJECT    // remove this

public:
    QLabel *label;

    test(QWidget *parent = 0) {
        resize(400, 300);
        label = new QLabel(this);
        label->setText("Test");
    };
};

Status update:

  • setStyleSheet("QWidget { background-color: white; }") does NOT solve the issue
  • I succeed in making every Widget (including popup dialogs) white, but that's not what I want.

回答1:


Ok, the proper answer can be found here, or alternatively by reading the documentation. I need to implement paintEvent for my test class:

class test : public QWidget
{
    Q_OBJECT    // remove this

public:
    QLabel *label;

    test(QWidget *parent = 0) {
        resize(400, 300);
        label = new QLabel(this);
        label->setText("Test");
    };

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

};

Also many thanks to 1+1=2 who read the manual for me at the Qt project forum.




回答2:


w.centralWidget->setStyleSheet("QWidget { background-color: white; }");



回答3:


There is a better way of doing this.

As you know, every QMainWindow has a central widget and by default is named centralwidget.

So the best way of solving this issue is changing the background for that widget.

It's pretty simple when we use a style sheet. In this case would be the following one:

#centralwidget {
    background-color: rgb(0, 0, 0);
}


来源:https://stackoverflow.com/questions/17860733/set-background-of-qmainwindow-central-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!