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. When setting the background of a QScrollArea to transparent, widgets inside lack their frames
Forum Updated to NodeBB v4.3 + New Features

When setting the background of a QScrollArea to transparent, widgets inside lack their frames

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 508 Views 2 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.
  • l3u_L Offline
    l3u_L Offline
    l3u_
    wrote on last edited by l3u_
    #1

    Hi all,

    "Is this a bug or a feature?!" – happens both with Qt 5.15.16 and Qt 6.8.2, using the Fusion style.

    When I put a QScrollArea inside a QGroupBox, the background is painted using a slightly lighter color than the group box's:
    1.png

    When I try to get id of this by explicitly setting the background to transparent, the different color is gone – but the widgets inside have their frames removed:
    2.png

    Cf. this minimal example:

    #include <QApplication>
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QGroupBox>
    #include <QScrollArea>
    #include <QLabel>
    #include <QPushButton>
    
    class Demo : public QWidget
    {
    public:
        Demo() {
            QVBoxLayout *layout;
            layout = new QVBoxLayout(this);
            auto *box = new QGroupBox(tr("Some Box"));
            layout->addWidget(box);
    
            layout = new QVBoxLayout(box);
            auto *scroll = new QScrollArea;
            scroll->setFrameShape(QFrame::NoFrame);
            layout->addWidget(scroll);
    
            // Uncomment to see the frames being gone
            //QPalette palette;
            //palette.setColor(QPalette::Window, Qt::transparent);
            //scroll->setPalette(palette);
    
            auto *widget = new QWidget;
            layout = new QVBoxLayout(widget);
            layout->addWidget(new QLabel(tr("Some Label")));
            layout->addWidget(new QPushButton(tr("Some Button")));
    
            scroll->setWidget(widget);
        }
    };
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
        Demo demo;
        demo.show();
        return app.exec();
    }
    

    What's going on here?! How do I get rid of that different color without losing the frames?

    Pl45m4P 1 Reply Last reply
    1
    • l3u_L l3u_

      Hi all,

      "Is this a bug or a feature?!" – happens both with Qt 5.15.16 and Qt 6.8.2, using the Fusion style.

      When I put a QScrollArea inside a QGroupBox, the background is painted using a slightly lighter color than the group box's:
      1.png

      When I try to get id of this by explicitly setting the background to transparent, the different color is gone – but the widgets inside have their frames removed:
      2.png

      Cf. this minimal example:

      #include <QApplication>
      #include <QWidget>
      #include <QVBoxLayout>
      #include <QGroupBox>
      #include <QScrollArea>
      #include <QLabel>
      #include <QPushButton>
      
      class Demo : public QWidget
      {
      public:
          Demo() {
              QVBoxLayout *layout;
              layout = new QVBoxLayout(this);
              auto *box = new QGroupBox(tr("Some Box"));
              layout->addWidget(box);
      
              layout = new QVBoxLayout(box);
              auto *scroll = new QScrollArea;
              scroll->setFrameShape(QFrame::NoFrame);
              layout->addWidget(scroll);
      
              // Uncomment to see the frames being gone
              //QPalette palette;
              //palette.setColor(QPalette::Window, Qt::transparent);
              //scroll->setPalette(palette);
      
              auto *widget = new QWidget;
              layout = new QVBoxLayout(widget);
              layout->addWidget(new QLabel(tr("Some Label")));
              layout->addWidget(new QPushButton(tr("Some Button")));
      
              scroll->setWidget(widget);
          }
      };
      
      int main(int argc, char *argv[]) {
          QApplication app(argc, argv);
          Demo demo;
          demo.show();
          return app.exec();
      }
      

      What's going on here?! How do I get rid of that different color without losing the frames?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @l3u_ said in When setting the background of a QScrollArea to transparent, widgets inside lack their frames:

      What's going on here?!

      I think the same color is used for the border and since you modified the palette it's also applied to the child widgets (ie. the button) of your scroll area.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • l3u_L Offline
        l3u_L Offline
        l3u_
        wrote on last edited by l3u_
        #3

        Why would the QPalette::Window ColorRole be used for a border?! Also, the background color of the QGroupBox is very different from the border color of the QPushButton …

        Pl45m4P 1 Reply Last reply
        0
        • l3u_L l3u_

          Why would the QPalette::Window ColorRole be used for a border?! Also, the background color of the QGroupBox is very different from the border color of the QPushButton …

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @l3u_

          See:

          • https://doc.qt.io/qt-6/qpalette.html#ColorRole-enum

          There is a limited amount of colors and settings in QPalette, so some controls/areas/widgets share the same role and therefore color.
          The lighter color for group box also has to do with this.

          @l3u_ said in When setting the background of a QScrollArea to transparent, widgets inside lack their frames:

          QPalette palette;
          palette.setColor(QPalette::Window, Qt::transparent);
          scroll->setPalette(palette);
          

          You construct an empty palette here and only set the QPalette::Window role... other roles are undefined.
          Specifying the role that is used for the button might solve this.

          Like

          auto scrollPalette = scroll->palette();
          scrollPalette.setColor(QPalette::Window, Qt::transparent);
          scroll->setPalette(scrollPalette);
          

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          l3u_L 1 Reply Last reply
          0
          • l3u_L Offline
            l3u_L Offline
            l3u_
            wrote on last edited by l3u_
            #5

            Yeah, I read the docs … "QPalette::Window: 10 A general background color" … This is really weird …

            However, one can work around this by setting the background (i.e. QPalette::Window) color to Qt::transparent and then set the style()->standardPalette() for all widgets inside tol get the original look back.

            1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @l3u_

              See:

              • https://doc.qt.io/qt-6/qpalette.html#ColorRole-enum

              There is a limited amount of colors and settings in QPalette, so some controls/areas/widgets share the same role and therefore color.
              The lighter color for group box also has to do with this.

              @l3u_ said in When setting the background of a QScrollArea to transparent, widgets inside lack their frames:

              QPalette palette;
              palette.setColor(QPalette::Window, Qt::transparent);
              scroll->setPalette(palette);
              

              You construct an empty palette here and only set the QPalette::Window role... other roles are undefined.
              Specifying the role that is used for the button might solve this.

              Like

              auto scrollPalette = scroll->palette();
              scrollPalette.setColor(QPalette::Window, Qt::transparent);
              scroll->setPalette(scrollPalette);
              
              l3u_L Offline
              l3u_L Offline
              l3u_
              wrote on last edited by
              #6

              You construct an empty palette here and only set the QPalette::Window role... other roles are undefined.
              Specifying the role that is used for the button might solve this.

              Using the scroll area's palette has the very same effect:

              auto palette = scroll->palette();
              palette.setColor(QPalette::Window, Qt::transparent);
              scroll->setPalette(palette);
              
              Pl45m4P 1 Reply Last reply
              0
              • l3u_L l3u_

                You construct an empty palette here and only set the QPalette::Window role... other roles are undefined.
                Specifying the role that is used for the button might solve this.

                Using the scroll area's palette has the very same effect:

                auto palette = scroll->palette();
                palette.setColor(QPalette::Window, Qt::transparent);
                scroll->setPalette(palette);
                
                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @l3u_

                Have you checked if QPalette::Window role is the correct role for your groupbox background?
                Maybe Midlight or Light is used there and you only see the effect at first because you wiped the whole palette config for your QScrollArea?


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                0
                • l3u_L Offline
                  l3u_L Offline
                  l3u_
                  wrote on last edited by
                  #8

                  Well, at least, when setting the QPalette::Window color, the background of the QScrollArea is painted using this very color – so I suppose it's the correct value.

                  However, this seems to only affect Fusion. When e.g. using KDE's Breeze, the widgets inside are not affected in the same way. So I suppose I have to use the workaround I found (setting the style's default palette for all widgets inside).

                  1 Reply Last reply
                  0
                  • l3u_L Offline
                    l3u_L Offline
                    l3u_
                    wrote on last edited by
                    #9

                    Another update to this:

                    One can prevent this by setting setAutoFillBackground(false), both for the scroll area's viewport() and the scroll widget set, after it has been set.

                    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