Setting the background color of widget inside another widget
-
*I am trying to write a very simple application that creates an application with a colored widget at the top of the screen. But all I get is a white screen.
I create the InnerWidget and set its minimum size to 1280x70. Then I use the style sheet to set the background color to black.*
#include "innerwidget.h" #include <QStyle> InnerWidget::InnerWidget(QWidget *parent) : QWidget(parent) { setMinimumSize(1280,70); this->setStyleSheet("background-color: black"); }
Then, in OuterWidget, I create the InnerWidget and add it to the OuterWidget in a QGridLayout.
#include "outerwidget.h" #include <QGridLayout> OuterWidget::OuterWidget(QWidget *parent) : QWidget(parent) { innerWidget = new InnerWidget(this); QGridLayout *layout = new QGridLayout; layout->addWidget(innerWidget, 0, 0, 0, 4, Qt::AlignTop); this->setLayout(layout); this->setWindowState(Qt::WindowFullScreen); } OuterWidget::~OuterWidget() { }
My main.cpp just creates the OuterWidget and shows it.
#include "outerwidget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); OuterWidget w; w.show(); return a.exec(); }
Why is the InnerWidget not appearing on the screen? Am I using the stylesheets or the layout incorrectly?
Thank you.
-
@JonB Thanks for the comment. I have tried adding the ";", but when I do I get a "Could not parse stylsheet of object _____" error and the screen looks the same. I think the reason the semi-colon is not needed is because I am not using a selector, I don't need to use a semicolon to mark the end of the statement.
-
@Smeeth I should note that when I add a control to the InnerWidget, it appears and has a black background. For example, I added the following code inside InnerWidget.
QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(new QLabel());
The QLabel appears in the middle of form and fills almost the whole screen with black (minus what I assume is the default margins or padding of the OuterWidget)