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. QWidget in QWidget overrides base widget stylesheet
Forum Updated to NodeBB v4.3 + New Features

QWidget in QWidget overrides base widget stylesheet

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 964 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
    Sucharek
    wrote on 15 Jun 2023, 15:05 last edited by
    #1

    I'm trying to make a child widget in a parent widget to have a different background, so I can know where it starts/ends, so I applied a stylsheet so that the background is different.
    There's a problem though. The child widget overrides the parent widget stylesheet.

    One more thing I should mention. I added a stylesheet in the designer and that seemed to work. Then promoted the widget, because I needed to add a functionality to it, but then the stylesheet just did nothing. So I tried implementing the stylesheet in my code, but even the parent widget gets affected.

    Here's what I have:

    controller::controller(QWidget *parent) : QWidget(parent)
    {
        parent->setStyleSheet("background-color: blue;"); //code to set the child widget's background color
    }
    

    This is how it looks in designer (don't mind the dark theme):
    7ada07e7-d0d7-4299-89e0-c02afb61f4bf-image.png
    This is what I actually have:
    9ebb1969-e6f8-4c9f-b46d-9b47efd3367a-image.png

    How can I fix it?

    J 1 Reply Last reply 15 Jun 2023, 15:14
    0
    • S Offline
      S Offline
      Sucharek
      wrote on 15 Jun 2023, 17:41 last edited by
      #6

      I ended up fixing it like this.
      I just edited it from the parent, because I have access to it from the .ui designer file.
      Here's the code:

      QPalette back;
      back.setColor(QPalette::Window, QColor(0, 0, 0));
      ui->area->setAutoFillBackground(true);
      ui->area->setPalette(back);
      

      That code is being executed when you open it from mainwindow.

      J 1 Reply Last reply 15 Jun 2023, 19:10
      0
      • S Sucharek
        15 Jun 2023, 15:05

        I'm trying to make a child widget in a parent widget to have a different background, so I can know where it starts/ends, so I applied a stylsheet so that the background is different.
        There's a problem though. The child widget overrides the parent widget stylesheet.

        One more thing I should mention. I added a stylesheet in the designer and that seemed to work. Then promoted the widget, because I needed to add a functionality to it, but then the stylesheet just did nothing. So I tried implementing the stylesheet in my code, but even the parent widget gets affected.

        Here's what I have:

        controller::controller(QWidget *parent) : QWidget(parent)
        {
            parent->setStyleSheet("background-color: blue;"); //code to set the child widget's background color
        }
        

        This is how it looks in designer (don't mind the dark theme):
        7ada07e7-d0d7-4299-89e0-c02afb61f4bf-image.png
        This is what I actually have:
        9ebb1969-e6f8-4c9f-b46d-9b47efd3367a-image.png

        How can I fix it?

        J Offline
        J Offline
        JonB
        wrote on 15 Jun 2023, 15:14 last edited by JonB
        #2

        @Sucharek said in QWidget in QWidget overrides base widget stylesheet:

        The child widget overrides the parent widget stylesheet.

        It should not do that per se. Any stylesheet a child has will override anything it inherits from a parent, but it will not alter the parent.

        I don't know where you have two widgets in what you show, what is parent, what is child. Have you put a layout on the parent onto which you add the child?

        If your child area fills the whole of the parent's area then its color does not "override" that of the parent, but all you will see is the child's color as it is on top.

        But your code reads:

        parent->setStyleSheet("background-color: blue;"); //code to set the child widget's background color
        

        That naturally does not set the child's stylesheet since you go parent->setStyleSheet()! So it does what you tell it to --- sets the parent's stylesheet/color. If you want to set the child's stylesheet then do so, don't make it set the parent's!

        S 1 Reply Last reply 15 Jun 2023, 15:56
        1
        • J JonB
          15 Jun 2023, 15:14

          @Sucharek said in QWidget in QWidget overrides base widget stylesheet:

          The child widget overrides the parent widget stylesheet.

          It should not do that per se. Any stylesheet a child has will override anything it inherits from a parent, but it will not alter the parent.

          I don't know where you have two widgets in what you show, what is parent, what is child. Have you put a layout on the parent onto which you add the child?

          If your child area fills the whole of the parent's area then its color does not "override" that of the parent, but all you will see is the child's color as it is on top.

          But your code reads:

          parent->setStyleSheet("background-color: blue;"); //code to set the child widget's background color
          

          That naturally does not set the child's stylesheet since you go parent->setStyleSheet()! So it does what you tell it to --- sets the parent's stylesheet/color. If you want to set the child's stylesheet then do so, don't make it set the parent's!

          S Offline
          S Offline
          Sucharek
          wrote on 15 Jun 2023, 15:56 last edited by Sucharek
          #3

          Hi @JonB, alright I tried, but it doesn't do anything.

          In the child widget initialization I added controller::setStyleSheet("background-color: blue;");

          That did not do anything. I also tried using QPallete, but that also doesn't work.

          Edit: Now it just looks normal
          907038d1-5c95-41d9-b004-1e54afe749f7-image.png

          J 1 Reply Last reply 15 Jun 2023, 16:21
          0
          • S Sucharek
            15 Jun 2023, 15:56

            Hi @JonB, alright I tried, but it doesn't do anything.

            In the child widget initialization I added controller::setStyleSheet("background-color: blue;");

            That did not do anything. I also tried using QPallete, but that also doesn't work.

            Edit: Now it just looks normal
            907038d1-5c95-41d9-b004-1e54afe749f7-image.png

            J Offline
            J Offline
            JonB
            wrote on 15 Jun 2023, 16:21 last edited by JonB
            #4

            @Sucharek said in QWidget in QWidget overrides base widget stylesheet:

            In the child widget initialization I added controller::setStyleSheet("background-color: blue;");

            I don't know what controller is, only you do. If it's the child you haven't said so.

            Anyway, there's not much to say. If the child is this then setStyleSheet("background-color: blue;"); sets its background color to blue. And does not affect the parent. Which is what you asked for.

            Maybe your child widget is of minimal size inside its parent, so all you see is the parent. I don't know. Maybe you should try adding some other widget (let's say, a non-empty QLabel) onto your child widget to see where it is.

            S 1 Reply Last reply 15 Jun 2023, 16:33
            1
            • J JonB
              15 Jun 2023, 16:21

              @Sucharek said in QWidget in QWidget overrides base widget stylesheet:

              In the child widget initialization I added controller::setStyleSheet("background-color: blue;");

              I don't know what controller is, only you do. If it's the child you haven't said so.

              Anyway, there's not much to say. If the child is this then setStyleSheet("background-color: blue;"); sets its background color to blue. And does not affect the parent. Which is what you asked for.

              Maybe your child widget is of minimal size inside its parent, so all you see is the parent. I don't know. Maybe you should try adding some other widget (let's say, a non-empty QLabel) onto your child widget to see where it is.

              S Offline
              S Offline
              Sucharek
              wrote on 15 Jun 2023, 16:33 last edited by
              #5

              @JonB, ok I'll give you more info.

              Control class is the parrent and controller class is the child.
              Calling controller::setStyleSheet("background-color: blue;"); does not do anything.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sucharek
                wrote on 15 Jun 2023, 17:41 last edited by
                #6

                I ended up fixing it like this.
                I just edited it from the parent, because I have access to it from the .ui designer file.
                Here's the code:

                QPalette back;
                back.setColor(QPalette::Window, QColor(0, 0, 0));
                ui->area->setAutoFillBackground(true);
                ui->area->setPalette(back);
                

                That code is being executed when you open it from mainwindow.

                J 1 Reply Last reply 15 Jun 2023, 19:10
                0
                • S Sucharek has marked this topic as solved on 15 Jun 2023, 17:41
                • S Sucharek
                  15 Jun 2023, 17:41

                  I ended up fixing it like this.
                  I just edited it from the parent, because I have access to it from the .ui designer file.
                  Here's the code:

                  QPalette back;
                  back.setColor(QPalette::Window, QColor(0, 0, 0));
                  ui->area->setAutoFillBackground(true);
                  ui->area->setPalette(back);
                  

                  That code is being executed when you open it from mainwindow.

                  J Offline
                  J Offline
                  JoeCFD
                  wrote on 15 Jun 2023, 19:10 last edited by
                  #7

                  @Sucharek Another way for this is to add object name to the style sheet. Then you do not need extra code.

                  1 Reply Last reply
                  1

                  1/7

                  15 Jun 2023, 15:05

                  • Login

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