C++ 如何为 Qt 应用程序的整个窗口设置背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1675992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:52:50  来源:igfitidea点击:

How do I set a background color for the whole window of a Qt application?

c++qtqstylesheetqwindow

提问by bryce

Does anyone know how one would be able to set a background color for the whole window of a Qt application?

有谁知道如何为 Qt 应用程序的整个窗口设置背景颜色?

So far I am using stylesheets but can only figure out how to assign a background color to a widget such as QGroupBoxor QPushButton. Basically, if I want a black background how would I make it seamless without any borders of the original background?

到目前为止,我正在使用样式表,但只能弄清楚如何为小部件分配背景颜色,例如QGroupBoxQPushButton。基本上,如果我想要黑色背景,我将如何使其无缝而没有原始背景的任何边框?

回答by Jér?me

I would simply use a Style Sheet for the whole window.

我会简单地为整个窗口使用样式表。

For instance, if your window is inheriting from QWidget, here is what I'm doing :

例如,如果您的窗口是从 QWidget 继承的,这就是我正在做的:

MainWindow::MainWindow(QWidget *parent) : QWidget(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setStyleSheet("background-color: black;");
}

On my Mac, my whole application window is black (except the title bar).

在我的 Mac 上,我的整个应用程序窗口都是黑色的(标题栏除外)。

EDIT : according to comment, here is a solution without using ui files and loading an external style sheet

编辑:根据评论,这是一个不使用 ui 文件和加载外部样式表的解决方案

#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QVBoxLayout>
#include <QtGui/QPushButton>
#include <QtCore/QFile>

int main(int ArgC, char* ArgV[])
{
QApplication MyApp(ArgC, ArgV);

QMainWindow* pWindow = new QMainWindow;
QVBoxLayout* pLayout = new QVBoxLayout(pWindow);
pWindow->setLayout(pLayout);

QPushButton* pButton = new QPushButton("Test", pWindow);
pLayout->addWidget(pButton);

QFile file(":/qss/default.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());

qApp->setStyleSheet(styleSheet);

pWindow->setVisible(true);
MyApp.exec();
}

The style sheet file (default.qss) is as follow :

样式表文件(default.qss)如下:

QWidget {
  background-color: black;
}

This file is part of a resource file (stylesheet.qrc) :

此文件是资源文件 (stylesheet.qrc) 的一部分:

<RCC>
  <qresource prefix="/qss">
    <file>default.qss</file>
  </qresource>
</RCC>

And here is my project file :

这是我的项目文件:

TARGET = StyleSheet
TEMPLATE = app
SOURCES += main.cpp
RESOURCES += stylesheet.qrc

回答by Dirk Eddelbuettel

This has worked for me:

这对我有用:

a = new QApplication(argc, argv);
QPalette pal = a->palette();
pal.setColor(QPalette::Window, Qt::white);
a->setPalette(pal);

回答by ajay

Simply just add

只需添加

setStyleSheet("background-color: white;");

to your code, you can give any color directly.

对于您的代码,您可以直接提供任何颜色。

回答by Nathan Campos

For the widgets I suggest you to see In Qt, how do I set the background color of a widget like combobox or double spin box?. Also check Custom Looks using Qt 4.2 Style Sheets. Remember that this second link shows you how to use the stylesheets in these widgets.

对于小部件,我建议您查看在 Qt 中,如何设置组合框或双旋转框等小部件的背景颜色?. 还要检查使用 Qt 4.2 样式表的自定义外观。请记住,第二个链接向您展示了如何使用这些小部件中的样式表。

If you have already developed something for Web and used CSS, it's the same thing.

如果您已经为 Web 开发了一些东西并使用了 CSS,那也是一样的。

回答by bvanlew

To set the background color the combination

设置背景颜色组合

setPaletteBackgroundColor(Qt::black);
setAutoFillBackground(true);

in the QWidget derived class worked for me. This is a variant of Dirk Eddelbuettel's solution but makes use of the function specifically for the background.

在 QWidget 派生类中为我工作。这是 Dirk Eddelbuettel 解决方案的变体,但专门为后台使用了该功能。