3

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
NelDav
  • 785
  • 2
  • 11
  • 30
  • You can add expanding (size policy) widget instead tableLayout, and set tableLayout for this widget. Chain like this: layout->widget->tableLayout – Deep Sep 06 '18 at 17:54
  • Apart from the typo, the code does exactly what it is told to: `btn1` on the first row spanning 3 rows and 3 columns, `btn2` on row 4 with the form layout right next to it. So, what is the problem exactly? – scopchanov Sep 06 '18 at 22:33
  • @scopchanov You are right this way it does what it is aspected to do. In my example I mixed the row and the column of `addWidget`. I am sorry for that. I changed it now. – NelDav Sep 07 '18 at 07:24
  • @Ahrtaler you say: *I already tried to put the QFormLayout into a own widget and add the widget to the QGridLayout. But it didn't changed anything.*. you could place the code you are using to do that, we call that [mcve], it would also be interesting to show an image of what you get and what you want to obtain. – eyllanesc Sep 07 '18 at 07:36
  • @eyllanesc Yes I will do that please wait a minute – NelDav Sep 07 '18 at 07:38
  • @Ahrtaler what you want to obtain? – eyllanesc Sep 07 '18 at 07:53
  • @eyllanesc Now I added picture of what I want to obtain – NelDav Sep 07 '18 at 08:06

1 Answers1

3

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Thank you very much! I totaly misunderstood the QGridLayout. I always thought, it is a even grid. And If you want to make one element bigger you have to add more columns and rows. But it is much better to use this way. Thank you that you spend so much time for me. – NelDav Sep 07 '18 at 08:41