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 the space in QVBoxLayout be set by the stylesheet, while only widgets like labels successfully set their background color?

Why can’t the space in QVBoxLayout be set by the stylesheet, while only widgets like labels successfully set their background color?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 242 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.
  • J Offline
    J Offline
    John Van
    wrote on last edited by
    #1

    I used setStyleSheet("QWidget{background:red}") in the widget, but the space in the VLayout is controlled by the parent widget above it. This is a multi-layered widget, and I’m not sure which level of the widget controls the color. I can only determine that the color setting at the lowest level has failed.Why is this the case?

    jsulmJ 1 Reply Last reply
    0
    • C ChrisW67

      In theory, the color of space in QVBoxLayout is controlled by the current widget, right?

      A QVBoxLayout is not a widget, just an agent to arrange them on the screen. Space you see between widgets in a layout is from whatever lies underneath (i.e. the widget on which the layout (might be nested) is ultimately applied). That widget may not honour the "background" attribute or be displaying that color in the places you are overlaying.

      We cannot see your "multi-layered widget", how your layouts apply or are nested, or exactly what pixels your query pertains to. It's hard to be specific about generalities.

      #include <QApplication>
      #include <QWidget>
      #include <QPushButton>
      #include <QVBoxLayout>
      
      int main(int argc, char **argv) {
              QApplication app(argc, argv);
      
              QWidget w;
              w.setStyleSheet("QWidget { background-color: red }");
      
              QVBoxLayout *layout = new QVBoxLayout(&w);
              for (int i = 0; i < 5; ++i) {
                      QPushButton *button = new QPushButton("Demo", &w);
                      layout->addWidget(button);
              }
              w.show();
      
              return app.exec();
      }
      

      Produces what one might expect:
      e4e2d45c-3558-440e-b43d-888899af9271-image.png

      J Offline
      J Offline
      John Van
      wrote on last edited by
      #6

      @ChrisW67
      Thank you for your reply. In the end, I used QFrame instead of QWidget, and I was able to set the color of the space in the BoxLayout using the stylesheet.

      1 Reply Last reply
      0
      • J John Van

        I used setStyleSheet("QWidget{background:red}") in the widget, but the space in the VLayout is controlled by the parent widget above it. This is a multi-layered widget, and I’m not sure which level of the widget controls the color. I can only determine that the color setting at the lowest level has failed.Why is this the case?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @John-Van Not sure I understand the problem: a widget can only apply a stylesheet on itself, it's not its job to alter its parent. Maybe it will help if you set margins to 0 to remove empty space around the widget inside the layout?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

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

          Do you mean a spacer item in the layout? It is not a widget, so it does not have any background color.

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @John-Van Not sure I understand the problem: a widget can only apply a stylesheet on itself, it's not its job to alter its parent. Maybe it will help if you set margins to 0 to remove empty space around the widget inside the layout?

            J Offline
            J Offline
            John Van
            wrote on last edited by
            #4

            @jsulm
            Yes, setSpacing (0) can solve this problem, but it may bring some other issues. In theory, the color of space in QVBoxLayout is controlled by the current widget, right?

            setStyleSheet("QWidget{background:red}");
            auto v0 = new QVBoxLayout(this);
            v0->setSpacing(30);
            
            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by ChrisW67
              #5

              In theory, the color of space in QVBoxLayout is controlled by the current widget, right?

              A QVBoxLayout is not a widget, just an agent to arrange them on the screen. Space you see between widgets in a layout is from whatever lies underneath (i.e. the widget on which the layout (might be nested) is ultimately applied). That widget may not honour the "background" attribute or be displaying that color in the places you are overlaying.

              We cannot see your "multi-layered widget", how your layouts apply or are nested, or exactly what pixels your query pertains to. It's hard to be specific about generalities.

              #include <QApplication>
              #include <QWidget>
              #include <QPushButton>
              #include <QVBoxLayout>
              
              int main(int argc, char **argv) {
                      QApplication app(argc, argv);
              
                      QWidget w;
                      w.setStyleSheet("QWidget { background-color: red }");
              
                      QVBoxLayout *layout = new QVBoxLayout(&w);
                      for (int i = 0; i < 5; ++i) {
                              QPushButton *button = new QPushButton("Demo", &w);
                              layout->addWidget(button);
                      }
                      w.show();
              
                      return app.exec();
              }
              

              Produces what one might expect:
              e4e2d45c-3558-440e-b43d-888899af9271-image.png

              J 1 Reply Last reply
              1
              • C ChrisW67

                In theory, the color of space in QVBoxLayout is controlled by the current widget, right?

                A QVBoxLayout is not a widget, just an agent to arrange them on the screen. Space you see between widgets in a layout is from whatever lies underneath (i.e. the widget on which the layout (might be nested) is ultimately applied). That widget may not honour the "background" attribute or be displaying that color in the places you are overlaying.

                We cannot see your "multi-layered widget", how your layouts apply or are nested, or exactly what pixels your query pertains to. It's hard to be specific about generalities.

                #include <QApplication>
                #include <QWidget>
                #include <QPushButton>
                #include <QVBoxLayout>
                
                int main(int argc, char **argv) {
                        QApplication app(argc, argv);
                
                        QWidget w;
                        w.setStyleSheet("QWidget { background-color: red }");
                
                        QVBoxLayout *layout = new QVBoxLayout(&w);
                        for (int i = 0; i < 5; ++i) {
                                QPushButton *button = new QPushButton("Demo", &w);
                                layout->addWidget(button);
                        }
                        w.show();
                
                        return app.exec();
                }
                

                Produces what one might expect:
                e4e2d45c-3558-440e-b43d-888899af9271-image.png

                J Offline
                J Offline
                John Van
                wrote on last edited by
                #6

                @ChrisW67
                Thank you for your reply. In the end, I used QFrame instead of QWidget, and I was able to set the color of the space in the BoxLayout using the stylesheet.

                1 Reply Last reply
                0
                • J John Van has marked this topic as solved on

                • Login

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