Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT grid layout top left



  • Hey,

    Is there any way to make a grid layout start filling by top-left so that I have something like this (only works because I have filled the grid)
    f4597ceb-93f3-4753-9087-703b5a7ab77f-image.png

    and not this
    b3f67569-cb30-4bab-a2fa-474af657cdad-image.png

    I have seen another answer in which someone created a custom layout but before I go that route I would like to know if there's something I can do in the designer itself.



  • @DevM
    Not so far as I know from usage (hence the other person creating a custom layout).

    EDIT I understood your question to be wanting to achieve this in Qt Designer!



  • I am not sure if I understood you right, but maybe this will help:

       GridLayout->addWidget(pBtn,1,1,1,1,{Qt::AlignTop, Qt::Alignleft});
    
    

    so the button would align in the top left corner.

    In another way you could expand the buttons vertically so they take up the full space of the grid layout depending on its size with something like:

    pBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
    

    You might wanna fix the size of the QGridLayout widget so it is filled.



  • @JackTr

    Your solution does somehow help or it gets close enough to what I want
    7a373bb7-cf62-49e4-8fc7-b9619761cf30-image.png

    code doing it:

        this->ui->gridLayout_2->addWidget(uc,1,1,1,1,{Qt::AlignTop,Qt::AlignLeft});
        this->ui->gridLayout_2->addWidget(new UC(this),1,2,{Qt::AlignTop,Qt::AlignLeft});
        this->ui->gridLayout_2->addWidget(new UC(this),1,3,{Qt::AlignTop,Qt::AlignLeft});
        this->ui->gridLayout_2->addWidget(new UC(this),1,4,{Qt::AlignTop,Qt::AlignLeft});
        this->ui->gridLayout_2->addWidget(new UC(this),2,1,{Qt::AlignTop,Qt::AlignLeft});
        this->ui->gridLayout_2->addWidget(new UC(this),2,2,{Qt::AlignTop,Qt::AlignLeft});
    

    Is it possible to change the spacing between those widgets though?


  • Lifetime Qt Champion

    @DevM said in QT grid layout top left:

    Is it possible to change the spacing between those widgets though?

    Hi
    Only by making the widgets bigger or the layout area smaller.

    I was wondering if this example layout is close to what you want ?
    https://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-example.html


Log in to reply