Important: Please read the Qt Code of Conduct -

How to set stretch in proper way in QGridLayout?

  • 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.


    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}");

  • @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)

  • But why my code doesn't works?

  • @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.

Log in to reply