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

Setting the background color of widget inside another widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 3.0k Views
  • 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 22 Jun 2018, 17:42 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.

    J 1 Reply Last reply 22 Jun 2018, 17:54
    0
    • S Smeeth
      22 Jun 2018, 17:42

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

      J Online
      J Online
      JonB
      wrote on 22 Jun 2018, 17:54 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 22 Jun 2018, 18:35
      0
      • J JonB
        22 Jun 2018, 17:54

        @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 22 Jun 2018, 18:35 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.

        J 1 Reply Last reply 22 Jun 2018, 18:44
        0
        • S Smeeth
          22 Jun 2018, 18:35

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

          J Online
          J Online
          JonB
          wrote on 22 Jun 2018, 18:44 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 22 Jun 2018, 19:14
          0
          • J JonB
            22 Jun 2018, 18:44

            @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 22 Jun 2018, 19:14 last edited by
            #5

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

            J S 2 Replies Last reply 22 Jun 2018, 19:18
            0
            • S Smeeth
              22 Jun 2018, 19:14

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

              J Online
              J Online
              JonB
              wrote on 22 Jun 2018, 19:18 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
                22 Jun 2018, 19:14

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

                S Offline
                S Offline
                Smeeth
                wrote on 22 Jun 2018, 19:47 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)

                J 1 Reply Last reply 22 Jun 2018, 20:04
                0
                • S Smeeth
                  22 Jun 2018, 19:47

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

                  J Online
                  J Online
                  JonB
                  wrote on 22 Jun 2018, 20:04 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 22 Jun 2018, 20:17
                  0
                  • J JonB
                    22 Jun 2018, 20:04

                    @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 22 Jun 2018, 20:17 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

                    1/9

                    22 Jun 2018, 17:42

                    • Login

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