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.



  • @Smeeth
    100% guess: this->setStyleSheet("background-color: black"); does it matter that you don't have a ; after black?



  • @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 the QGridLayout 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.



  • @Smeeth
    So you can eliminate the QGridLayout from the problem....

    ...Let's eliminate the InnerWidget code. Replace it with, I don't know, a QPushButon or something. Does that end up appearing?



  • @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)





  • @JonB @Smeeth asked the question on SO, and I found setting the stylesheet on the parentWidget worked.

    He asked why and I couldn't answer, hence turning to this forum


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.