如何在布局qt上销毁小部件

时间:2015-05-12 11:52:06

标签: c++ qt

我正在创建一个动态生成带布局的QgroupeBox的界面,我得到了所需的数量,这完全有效。现在我想使用一个按钮来销毁这些groupBoxes以获得相同动作的重复部分(获取所需的groupeBox数量并创建它们)

这是我用于此的代码:

void interface::on_pushButton_12_clicked(){
int i,k;
ui->pushButton_13->setEnabled(true);
QGroupBox *first = new QGroupBox(this);
QVBoxLayout *test = new QVBoxLayout;

k = recupEdit();

ui->pushButton_12->hide();
ui->lineEdit->hide();
ui->label->hide();

for(i = 1; i<=k;i++)
   {
    first = creatgroupebox();

    test->addWidget(first);
    test->addStretch(1);
    ui->tab->setLayout(test);
}
}
int interface::recupEdit(){
int k;
QString recup = ui->lineEdit->text();

k = recup.toInt(0,10);
return k;
}
QGroupBox *interface::creatgroupebox()
{
QGroupBox *group = new QGroupBox(this);
QLineEdit *Id = new QLineEdit("Id");
QLineEdit *Data1 = new QLineEdit("Data 1");
QLineEdit *Data2 = new QLineEdit("Data 2");
QLineEdit *Data3 = new QLineEdit("Data 3");
QLineEdit *Data4 = new QLineEdit("Data 4");
QLineEdit *Data5 = new QLineEdit("Data 5");
QLineEdit *Data6 = new QLineEdit("Data 6");
QLineEdit *Data7 = new QLineEdit("Data 7");
QLineEdit *Data8 = new QLineEdit("Data 8");


QHBoxLayout *layout = new QHBoxLayout;

layout->addWidget(Id);
layout->addWidget(Data1);
layout->addWidget(Data2);
layout->addWidget(Data3);
layout->addWidget(Data4);
layout->addWidget(Data5);
layout->addWidget(Data6);
layout->addWidget(Data7);
layout->addWidget(Data8);

group->setLayout(layout);



return group;
}
void interface::on_pushButton_13_clicked()
{

ui->pushButton_12->show();
ui->lineEdit->show();
ui->label->show();
}

1 个答案:

答案 0 :(得分:0)

您可以使用QObject :: children()

获取所有子对象
auto gb = new QGroupBox();
gb->setLayout(new QHBoxLayout());
gb->layout()->addWidget(new QLineEdit());

foreach (QObject *o, gb->layout()->children()) {
    auto le = qobject_cast<QLineEdit*>(o);
    if (!le)
        continue;
    //do what you need with your linedit
}

这是你想要的吗?