我正在创建一个包含以下项目的QFormLayout:
QFormLayout *tableLayout = new QFormLayout();
QLineEdit *line1 = new QLineEdit();
QLineEdit *line2 = new QLineEdit();
tableLayout->addRow(tr("LineText1 "), line1);
tableLayout->addRow(tr("LineText2 "), line2);
之后,我尝试将此布局添加到 QGridLayout 中,如下所示:
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);
在我添加了TableLayout之后,按钮1的宽度为1列,而TableLayout的宽度为3列。
我已经尝试将QFormLayout放入自己的小部件中,并将该小部件添加到QGridLayout中。但是这并没有改变任何东西。我操作的方式如下:
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);
这种奇怪的情况是什么原因造成的?如何解决它呢?
以下是结果的图片:
![enter image description here](https://istack.dev59.com/dOjyw.webp)
![enter image description here](https://istack.dev59.com/4vEev.webp)
btn1
位于第一行,跨越3行和3列,btn2
位于第4行,紧挨着表单布局。那么问题到底是什么? - scopchanovaddWidget
的行和列。对此我感到抱歉。我已经进行了更改。 - NelDav