设置QMainWindow中央小部件的背景 [英] Set background of QMainWindow central widget

查看:227
本文介绍了设置QMainWindow中央小部件的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows 7(MSVC 2010)上使用Qt 4.8.4我的应用程序中带有工具栏,有一个标准的QMainWindow.我希望工具栏保持灰色,但中央窗口小部件应具有白色背景.最初调用centralWidget->setStyleSheet("background-color: white;")似乎可以完成这项工作,但不能将其与Designer生成的小部件(Q_OBJECT)一起使用.随后,我玩弄了其他各种方法来设置样式表(也使用Designer),但无济于事.

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.

要查看此效果,请添加或删除test.h中的Q_OBJECT行.在那里时,只有标签会变成白色bg.如果将Q_OBJECT注释掉,则整个中央窗口小部件为白色.当然,我希望整个区域都是白色的,但也需要Q_OBJECT.

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.

以下是文件:

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

状态更新:

  • setStyleSheet("QWidget { background-color: white; }")无法解决问题
  • 我成功地将每个小部件(包括弹出对话框)设置为白色,但这不是我想要的.
  • 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.

推荐答案

好的,可以找到正确的答案

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

};

非常感谢1 + 1 = 2在 Qt上为我阅读了手册项目论坛.

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

这篇关于设置QMainWindow中央小部件的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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