Setting the background color of widget inside another widget
-
wrote on 22 Jun 2018, 17:42 last edited by Smeeth
*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.
-
*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.
-
@Smeeth
100% guess:this->setStyleSheet("background-color: black");
does it matter that you don't have a;
afterblack
?wrote on 22 Jun 2018, 18:35 last edited by@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.
-
@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
While you're waiting for someone more observant/useful than I am :), if you get rid of theQGridLayout
does it make any difference? That's what I'd try next.... -
@JonB I appreciate your suggestions! I switched to a simple QVBoxLayout but the problem persists.
-
@JonB I appreciate your suggestions! I switched to a simple QVBoxLayout but the problem persists.
wrote on 22 Jun 2018, 19:47 last edited by Smeeth@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)
-
@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)
wrote on 22 Jun 2018, 20:04 last edited by@Smeeth
You and @skebanga are working on the same code?
https://forum.qt.io/topic/91920/setstylesheet-on-qwidget-does-not-have-effect -
@Smeeth
You and @skebanga are working on the same code?
https://forum.qt.io/topic/91920/setstylesheet-on-qwidget-does-not-have-effect
1/9