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. Why Can't I change background-color in QWidget using CSS?
QtWS25 Last Chance

Why Can't I change background-color in QWidget using CSS?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.8k 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by
    #1

    Hello,

    I have 3 classes:
    myWid
    newWid
    widLab

    In newWid and widLab I only create QGridLayouts and setLayouts(), nothing more:

    newWid::newWid(QWidget *parent):QWidget(parent)
    {
        grid = new QGridLayout;
        setLayout(grid);
    }
    
    widLab::widLab(QWidget *parent):QWidget(parent)
    {
        grid = new QGridLayout;
        setLayout(grid);
    }
    

    In class myWid In constructor I have:

    myWid::myWid(QWidget *parent): QWidget(parent)
    {
        grid = new QGridLayout;
    
        nw = new newWid;
        wl = new widLab;
    
        setLayout(grid);
    
        grid->addWidget(nw,0,0,1,1);
        grid->addWidget(wl,0,1,1,1);
        setStyleSheet("QWidget {background-color:red}");
        nw->setStyleSheet("QWidget {background-color:red}");
        wl->setStyleSheet("QWidget {background-color:red}");
    }
    

    And that 3 classes don't have any other methods. Only constructors.

    Next in mainWindow constructor ( which doesn't have other methods ) I have:

        mw = new myWid;
    
        grid = new QGridLayout;
        centralWidget()->setLayout(grid);
        grid->addWidget(mw);
    

    Classes myWid, newWid, widLab inherits QWidget.

    And there is no color red. I try use that setStyleSheet in constructors too, but with the same result ( no result ).

    Of course when I add to grids in newWid and widLab widgets I see them, but no on the red background.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #4

      A subclassed QWidget need to either call

      setAttribute(Qt::WA_StyledBackground);
      

      or reimplement paintEvent as stated in the doc:

        void CustomWidget::paintEvent(QPaintEvent *)
        {
            QStyleOption opt;
            opt.init(this);
            QPainter p(this);
            style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
        }
      
      1 Reply Last reply
      4
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #2

        I guess that when you assign grid->addWidget(mw); your child widgets inherit the QSS from parent and thus loose your custom stylesheet. Setup your UI first, then set QSS.

        (Z(:^

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TomNow99
          wrote on last edited by
          #3

          @sierdzio
          In mainWindow constructor I add ( after grid->addWidget(mw) )

               QTimer::singleShot(100,this, SLOT(x()));
          

          and the x() slot looks like:

          void MainWindow::x()
          {
              mw->setStyleSheet("QWidget {background-color:red}");
          }
          

          but still doesn't get the red color.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #4

            A subclassed QWidget need to either call

            setAttribute(Qt::WA_StyledBackground);
            

            or reimplement paintEvent as stated in the doc:

              void CustomWidget::paintEvent(QPaintEvent *)
              {
                  QStyleOption opt;
                  opt.init(this);
                  QPainter p(this);
                  style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
              }
            
            1 Reply Last reply
            4
            • T Offline
              T Offline
              TomNow99
              wrote on last edited by
              #5

              @Bonnie Yes, now it works! Thank you. But when I tested something like:

              in MainWindow

                  someObjectWhichInheritsQWidget = new someClass;
                  someObjectWhichInheritsQWidget -> setStyleSheet("QWidget {background-color:red}");
              
                  grid = new QGridLayout;
                  centralWidget()->setLayout(grid);
                  grid->addWidget(someObjectWhichInheritsQWidget);
              

              and in this someObjectWhichInheritsQWidget didn't had any other grids / widgets ( like in myWid ) everything was ok.

              So when I have to add

              setAttribute(Qt::WA_StyledBackground);
              

              ?

              B 1 Reply Last reply
              0
              • T TomNow99

                @Bonnie Yes, now it works! Thank you. But when I tested something like:

                in MainWindow

                    someObjectWhichInheritsQWidget = new someClass;
                    someObjectWhichInheritsQWidget -> setStyleSheet("QWidget {background-color:red}");
                
                    grid = new QGridLayout;
                    centralWidget()->setLayout(grid);
                    grid->addWidget(someObjectWhichInheritsQWidget);
                

                and in this someObjectWhichInheritsQWidget didn't had any other grids / widgets ( like in myWid ) everything was ok.

                So when I have to add

                setAttribute(Qt::WA_StyledBackground);
                

                ?

                B Offline
                B Offline
                Bonnie
                wrote on last edited by Bonnie
                #6

                @TomNow99
                AFAIK, a window or a QFrame doesn't need to add extra codes.
                Not sure what your case is.

                1 Reply Last reply
                2
                • T Offline
                  T Offline
                  TomNow99
                  wrote on last edited by
                  #7

                  @Bonnie You are right.

                  I create 2 widgets in mainWindow:
                  QWidget object
                  myClassWhichInheritsQWidget object

                  In QWidget I don't need add extra code, but in myClassWhi.... object I have to add that

                  setAttribute(Qt::WA_StyledBackground);
                  

                  Thank you

                  1 Reply Last reply
                  0

                  • Login

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