29

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

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

ymoreau
  • 3,402
  • 1
  • 22
  • 60
bryce
  • 403
  • 1
  • 5
  • 14

5 Answers5

31

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 :

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).

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

#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 :

QWidget {
  background-color: black;
}

This file is part of a resource file (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
Jérôme
  • 26,567
  • 29
  • 98
  • 120
  • How do I do this in a stylesheet? I've done this for widgets like the QPushButton but cannot figure out what to use for the background as a whole. – bryce Nov 06 '09 at 12:44
  • Let me clarify on a few things too. I am not using a .ui file from designer since I hand coded everything. Also, I have a separate stylesheet .qss file versus putting the code directly into the source code. – bryce Nov 06 '09 at 12:52
  • Thanks, I knew it was right under my nose. QWidget { background-color: black;} was what I needed. I didn't realized I could directly use QWidget. – bryce Nov 06 '09 at 16:24
16

This has worked for me:

a = new QApplication(argc, argv);
QPalette pal = a->palette();
pal.setColor(QPalette::Window, Qt::white);
a->setPalette(pal);
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • It has been my experience that for most palette roles, you don't even need to get the original palette. Setting just one role on a default constructed palette, and setting that palette for the widget, will only change the role you specified. – Caleb Huitt - cjhuitt Nov 05 '09 at 17:46
  • I believe now you also have to add something like `this->setAutoFillBackground(true);` to get this to work. It's what I had to do when setting a default background color for a widget this way. – gnovice Dec 22 '11 at 21:14
5

Simply just add

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

to your code, you can give any color directly.

Neji
  • 6,591
  • 5
  • 43
  • 66
ajay
  • 51
  • 1
  • 1
2

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.

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

Community
  • 1
  • 1
Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
0

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.

bvanlew
  • 1,465
  • 16
  • 13