Search code examples
c++qtqt5qgridlayoutqformlayout

QFormLayout expands column of QGridLayout


I am creating a QFormLayout with some items like this:

QFormLayout *tableLayout = new QFormLayout();
QLineEdit *line1 = new QLineEdit();
QLineEdit *line2 = new QLineEdit();

tableLayout->addRow(tr("LineText1 "), line1);
tableLayout->addRow(tr("LineText2 "), line2);

After that I try to add this Layout to a QGridLayout like this:

QGridLayout *layout = new QGridLayout();
QPushButton *btn1 = new QPushButton();
QPushButton *btn2 = new QPushButton();
layout->addWidget(btn, 1, 1, 3, 3);
layout->addWidget(btn2, 1, 4);
layout->addLayout(tableLayout, 2, 4);

After I added the tableLayout, btn1 as width as 1 column and the tableLayout is as width as 3 columns.

I already tried to put the QFormLayout into a own widget and add the widget to the QGridLayout. But it didn't changed anything. The way I am doing that is the following:

QFormLayout *tableLayout = new QFormLayout();
QLineEdit *line1 = new QLineEdit();
QLineEdit *line2 = new QLineEdit();

tableLayout->addRow(tr("LineText1 "), line1);
tableLayout->addRow(tr("LineText2 "), line2);

QWidget *widget = new QWidget();
widget->setLayout(tableLayout);


QGridLayout *layout = new QGridLayout();
QPushButton *btn1 = new QPushButton();
btn1->setText("btn1");
QPushButton *btn2 = new QPushButton();
btn2->setText("btn2");
layout->addWidget(btn1, 1, 1, 3, 3);
layout->addWidget(btn2, 1, 4);
layout->addWidget(widget, 2, 4);

What is the reason for this strange situation? And how to solve it?

Here is a picture of the result:enter image description here

And here is wat I want to have:enter image description here


Solution

  • To build the design you want the first thing is to establish the position of the elements, remember that the position of the rows or columns start at 0, not at 1 as you do. The second part is to set the size policies, some widgets already have some established policy such as the QPushButton that stretches horizontally but not vertically so even if the rowSpan is large it will not change the height of the button, so we must change that behavior and finally the stretch.

    #include <QApplication>
    #include <QFormLayout>
    #include <QLineEdit>
    #include <QPushButton>
    #include <QSizePolicy>
    #include <QWidget>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QWidget w;
    
        QGridLayout *layout = new QGridLayout(&w);
    
        QPushButton *btn1 = new QPushButton("Btn1");
        btn1->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    
        QPushButton *btn2 = new QPushButton("Btn2");
        btn2->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    
        QFormLayout *tableLayout = new QFormLayout();
        QLineEdit *line1 = new QLineEdit();
        QLineEdit *line2 = new QLineEdit();
        tableLayout->addRow("LineText1 ", line1);
        tableLayout->addRow("LineText2 ", line2);
    
        layout->addWidget(btn1, 0, 0, 3, 3);
        layout->addWidget(btn2, 0, 3);
        layout->addLayout(tableLayout, 1, 3);
    
        // column 0 x3
        layout->setColumnStretch(0, 3);
        // column 3 x1
        layout->setColumnStretch(3, 1);
    
        w.resize(640, 480);
        w.show();
    
        return a.exec();
    }
    

    enter image description here

    Note that the QFormLayout will make the widgets always on top, so it will not necessarily occupy the height of the space offered by the QGridLayout.