简体   繁体   中英

how to destroy widgets on a layout qt

i'm working to creat an interface that dynamically generates QgroupeBox with layouts, i get the number of groupebox needed and it perfectly work. now i want to use a button to destroy these groupBoxes for a repeat section of the same action (get the number of groupeBox needed and creat them)

this is the code i used for this:

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();
}

You can get all child objects by using 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
}

Is it what you want?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM