1、删除布局
图1
QLayout *p = ui->verticalLayout_7->itemAt(0)->layout();
while(p->count()){
QWidget *pWidget = p->itemAt(0)->widget();
pWidget->setParent(NULL);
p->removeWidget(pWidget);
delete pWidget;
}
ui->verticalLayout_7->removeItem(ui->verticalLayout_7->itemAt(0));
图2
QWidget *p=this->ui->verticalLayout->itemAt(0)->widget();
p->setParent (NULL); // 父控件置空
this->ui->verticalLayout->removeWidget(p);
delete p; // 清除内存
2、获取内容
图1
QStringList keyJson, valueJon;
int a = ui->verticalLayout_7->count(); // 获取该布局下的布局数
int i = 0;
while(a)
{
QLayoutItem *p=this->ui->verticalLayout_7->itemAt(i);
int count = p->layout()->count(); // 获取该布局下的布局类的控件数
int j = 0;
while(count){
if(j == 1){
QLayoutItem *pw = p->layout()->itemAt(j);
QLineEdit *le = qobject_cast<QLineEdit *>(pw->widget());
keyJson.append(le->text());
}
else if(j == 3){
QLayoutItem *pw = p->layout()->itemAt(j);
QLineEdit *le = qobject_cast<QLineEdit *>(pw->widget());
valueJson.append(le->text());
}
j++;
count--;
}
i++;
a--;
}
图2
QStringList strList;
int a = ui->verticalLayout->count();
qDebug() << a;
int i = 0;
QStringList strList;
while(a)
{
QLayoutItem *p=this->ui->verticalLayout->itemAt(i);
QTextEdit *le = qobject_cast<QTextEdit *>(p->widget());
strList.append(le->toPlainText());
i++;
a--;
}
3、增加布局
图1
QLabel *lKey = new QLabel("KEY:", this);
QLineEdit *leKey = new QLineEdit(this);
QLabel *lValue = new QLabel("VALUE:", this);
QLineEdit *leValue = new QLineEdit(this);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(lKey);
hbox->addWidget(leKey);
hbox->addWidget(lValue);
hbox->addWidget(leValue);
ui->verticalLayout_7->addLayout(hbox);
图2
QTextEdit *leNew = new QTextEdit(this);
ui->verticalLayout->addWidget(leNew);
原文链接
QT之获取布局内容及删除布局-QT开发中文网QT之获取布局内容及删除布局https://qt.0voice.com/?id=1005