具体代码示例如下 :
QHBoxLayout* hLayout = new QHBoxLayout;
hLayout->addWidget(new QLabel("444"));
hLayout->addWidget(new QLineEdit("444"));
hLayout->addWidget(new QPushButton("444"));
layout->addLayout(hLayout);
QHBoxLayout* hLayout2 = new QHBoxLayout;
hLayout2->addWidget(new QLabel("123"));
hLayout2->addWidget(new QLineEdit("123"));
hLayout2->addWidget(new QPushButton("123"));
layout->addLayout(hLayout2);
ui->widget->setLayout(layout);
QHBoxLayout* deleteHLayout = dynamic_cast<QHBoxLayout*>(layout->itemAt(0)->layout());
if(!deleteHLayout)
return;
QLayoutItem* child;
while((child = deleteHLayout->takeAt(0)) != nullptr)
{
delete child->widget(); // 删除对象
delete child; // 删除布局
}
delete deleteHLayout;
运行效果展示:
完整效果:
删除第一个布局之后效果: