changing window background color
-
Hi,
I am trying to change the appearance of the windows produced by QT.
I tried using style sheets to change the background color of a window, as follows:
QApplication *app = new QApplication(argc, argv); Control *control = new Control(); app->setStyleSheet("Control { background: black;} ");
That works until I add widgets to the control window. Then the window background becomes white again.
auto *vbox = new QVBoxLayout(); auto *btn1 = new QPushButton("button 1"); vbox->addWidget(btn1); auto *lbl1 = new QLabel("label 1"); vbox->addWidget(lbl1); setLayout(vbox);
How do I keep background color when I add buttons to the window?
Regards,
Jed
-
Hi and welcome to devnet,
Can you provide a minimal compilable example that shows that behaviour ?
Which version of Qt is it ?
On which OS ? -
I am using manjaro linux with the XFCE desktop. QT version 5.15.2.
The three program files are as follows:
main.cpp:
#include <QApplication> #include <QWidget> #include "control.h" int main(int argc, char **argv) { QApplication *app = new QApplication(argc, argv); Control *control = new Control(); QString style1 = "Control {background: black; } "; app->setStyleSheet(style1); control->move(500,300); control->setGeometry(300,300,400,500); control -> show(); return app -> exec(); }
control.h:
#pragma once #include <QWidget> #include <QPushButton> class Control : public QWidget { Q_OBJECT public: Control(QWidget *parent = 0); };
control.cpp:
#include "control.h" #include <QVBoxLayout> #include <QPushButton> #include <QLabel> Control::Control(QWidget *parent) : QWidget(parent) { setWindowTitle("Control Window"); auto *vbox = new QVBoxLayout(); /* // make buttons auto *btn1 = new QPushButton("button 1"); vbox->addWidget(btn1); auto *btn2 = new QPushButton("button 2"); vbox->addWidget(btn2); // make labels auto *lbl1 = new QLabel("label 1"); vbox->addWidget(lbl1); */ setLayout(vbox); }
If this is compiled as is, the background is black, but if you add in the commented out code, the background reverts to white.
-
It's working fine for me with the same version of Qt on macOS 10.13.6.
Is this the Qt version provided with your distribution ?