Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    7
    1327
    Loading More Posts
    • 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
      TomNow99 last edited by

      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 Reply Quote 0
      • B
        Bonnie last edited by Bonnie

        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 Reply Quote 4
        • sierdzio
          sierdzio Moderators last edited by

          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 Reply Quote 0
          • T
            TomNow99 last edited by

            @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 Reply Quote 0
            • B
              Bonnie last edited by Bonnie

              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 Reply Quote 4
              • T
                TomNow99 last edited by

                @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 Reply Quote 0
                • B
                  Bonnie @TomNow99 last edited by Bonnie

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

                  1 Reply Last reply Reply Quote 2
                  • T
                    TomNow99 last edited by

                    @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 Reply Quote 0
                    • First post
                      Last post