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. How to set stretch in proper way in QGridLayout?

How to set stretch in proper way in QGridLayout?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 4.3k 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

    Hi,

    I would like to have a red QGridLayout. In this grid I would like to have a green QGridLayout and blue GridLayout.
    In this blue QGridLayout I would like to have 4 QLabels ( yellow ).

    I would like to set 3 in the first ( zero if we start from zero ) row stretch in red QGridLayout and I would like don't add any widgets to green QGridLayout.
    I would like to set 2 in the second ( first if we start from zero) row stretch in red QGridLayout.

    So my goal is have picture on the left. Now I have situation on the right picture.

    want.png

    My code:
    ( whole - red QGridLayout, info - blue, images - green, l1,l2,l3,l4 - labels )

        whole = new QGridLayout;
        info = new QGridLayout;
        images = new QGridLayout;
    
        l1 = new QLabel("aaaaaa");
        l2 = new QLabel("bbbbbbb");
        l3 = new QLabel("cccccccc");
        l4 = new QLabel("ddddddd");
    
        l1->setStyleSheet("QLabel {background:red}");
        l2->setStyleSheet("QLabel {background:red}");
        l3->setStyleSheet("QLabel {background:red}");
        l4->setStyleSheet("QLabel {background:red}");
    
        l1->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
        l2->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
        l3->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
        l4->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
    
        info->addWidget(l1,0,0);
        info->addWidget(l2,0,1);
        info->addWidget(l3,1,0);
        info->addWidget(l4,1,1);
    
        whole->addLayout(images,0,0);
        whole->addLayout(info,1,0);
    
        whole->setRowStretch(0,3);
        whole->setRowStretch(1,2);
    
    Pl45m4P 1 Reply Last reply
    0
    • T TomNow99

      Hi,

      I would like to have a red QGridLayout. In this grid I would like to have a green QGridLayout and blue GridLayout.
      In this blue QGridLayout I would like to have 4 QLabels ( yellow ).

      I would like to set 3 in the first ( zero if we start from zero ) row stretch in red QGridLayout and I would like don't add any widgets to green QGridLayout.
      I would like to set 2 in the second ( first if we start from zero) row stretch in red QGridLayout.

      So my goal is have picture on the left. Now I have situation on the right picture.

      want.png

      My code:
      ( whole - red QGridLayout, info - blue, images - green, l1,l2,l3,l4 - labels )

          whole = new QGridLayout;
          info = new QGridLayout;
          images = new QGridLayout;
      
          l1 = new QLabel("aaaaaa");
          l2 = new QLabel("bbbbbbb");
          l3 = new QLabel("cccccccc");
          l4 = new QLabel("ddddddd");
      
          l1->setStyleSheet("QLabel {background:red}");
          l2->setStyleSheet("QLabel {background:red}");
          l3->setStyleSheet("QLabel {background:red}");
          l4->setStyleSheet("QLabel {background:red}");
      
          l1->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
          l2->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
          l3->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
          l4->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
      
          info->addWidget(l1,0,0);
          info->addWidget(l2,0,1);
          info->addWidget(l3,1,0);
          info->addWidget(l4,1,1);
      
          whole->addLayout(images,0,0);
          whole->addLayout(info,1,0);
      
          whole->setRowStretch(0,3);
          whole->setRowStretch(1,2);
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @TomNow99

      If you dont want to make further changes (overall result should look like the example on the left), you dont need three QGridLayouts. Only the blue one has to be a grid.

      • Red (VBoxLayout)
        • Green Layout ( 1/2 size of red)
        • Blue (GridLayout for yellow content) (1/2 size of red)

      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
      • T Offline
        T Offline
        TomNow99
        wrote on last edited by
        #3

        But why my code doesn't works?

        B 1 Reply Last reply
        0
        • T TomNow99

          But why my code doesn't works?

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

          @TomNow99
          Your drawing is vivid as usual :)
          It seems that the stretch factor will be ignored for an empty layout.
          If you really need to put an empty square here, I suggest you to use a QWidget instead of just a QGridLayout.
          You can set a QGridLayout to that QWidget if you need to add items to the square later.

          1 Reply Last reply
          1

          • Login

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