Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting the background color of widget inside another widget
Forum Updated to NodeBB v4.3 + New Features

Setting the background color of widget inside another widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Smeeth
    wrote on last edited by Smeeth
    #1

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

    JonBJ 1 Reply Last reply
    0
    • S 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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      S 1 Reply Last reply
      0
      • JonBJ JonB

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

        S Offline
        S Offline
        Smeeth
        wrote on last edited by
        #3

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

        JonBJ 1 Reply Last reply
        0
        • S Smeeth

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

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

          S 1 Reply Last reply
          0
          • JonBJ JonB

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

            S Offline
            S Offline
            Smeeth
            wrote on last edited by
            #5

            @JonB I appreciate your suggestions! I switched to a simple QVBoxLayout but the problem persists.

            JonBJ S 2 Replies Last reply
            0
            • S Smeeth

              @JonB I appreciate your suggestions! I switched to a simple QVBoxLayout but the problem persists.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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?

              1 Reply Last reply
              0
              • S Smeeth

                @JonB I appreciate your suggestions! I switched to a simple QVBoxLayout but the problem persists.

                S Offline
                S Offline
                Smeeth
                wrote on last edited by Smeeth
                #7

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

                JonBJ 1 Reply Last reply
                0
                • S 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)

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Smeeth
                  You and @skebanga are working on the same code?
                  https://forum.qt.io/topic/91920/setstylesheet-on-qwidget-does-not-have-effect

                  S 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Smeeth
                    You and @skebanga are working on the same code?
                    https://forum.qt.io/topic/91920/setstylesheet-on-qwidget-does-not-have-effect

                    S Offline
                    S Offline
                    skebanga
                    wrote on last edited by
                    #9

                    @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

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved