I have a QWidget in my Window "AvailableDataWidget" while runtime I add layouts with groupboxes, labels and checkboxes. I want to delete them later and add new groupboxes and checkboxes.

This is the way I create it:

Qt Code:
  1. void MonitorWindow::on_actionGetAvailableData_triggered(){
  2. QVBoxLayout* verticalLayout[NumberOfAvailableTypes];
  3. QVBoxLayout* verticalLayoutForWidget = new QVBoxLayout(ui->AvailableDataWidget);
  4.  
  5. QGroupBox *TypeGroupBox[NumberOfAvailableTypes];
  6.  
  7. for (int i = 0; i < NumberOfAvailableTypes; i++){ //1 groupbox for each type
  8. TypeGroupBox[i] = new QGroupBox(ui->AvailableDataWidget);
  9. TypeGroupBox[i]->setTitle(DataList.at(i).name);
  10. verticalLayout[i] = new QVBoxLayout(TypeGroupBox[i]);
  11.  
  12. if (DataList.at(i).type != TYPEBINARY){
  13. QCheckBox *DataCheckbox[DataList.at(i).quantity];
  14. QLabel* tempLabel[DataList.at(i).quantity];
  15. QHBoxLayout* horizontalLayout[DataList.at(i).quantity];
  16.  
  17. for (int j = 1; j <= DataList.at(i).quantity; j++){
  18. DataCheckbox[j] = new QCheckBox(TypeGroupBox[i]);
  19. DataCheckbox[j]->setChecked(false);
  20. connect(DataCheckbox[j],SIGNAL(clicked()),SLOT(setTransducerFlags()));
  21. horizontalLayout[j] = new QHBoxLayout();
  22. horizontalLayout[j]->addWidget(DataCheckbox[j]);
  23.  
  24. tempLabel[j] = new QLabel(TypeGroupBox[i]);
  25. tempLabel[j]->setText(QString("%1%2%3").arg(DataList.at(i).name).arg(" ").arg(j));
  26. horizontalLayout[j]->addWidget(tempLabel[j]);
  27.  
  28. verticalLayout[i]->addLayout(horizontalLayout[j]);
  29. }
  30. }
  31. else{
  32. ...
  33. QCheckBox *DataCheckbox = new QCheckBox(TypeGroupBox[i]);
  34. DataCheckbox->setChecked(false);
  35. connect(DataCheckbox,SIGNAL(clicked()),SLOT(setTransducerFlags()));
  36. QLabel* tempLabel = new QLabel(TypeGroupBox[i]);
  37. tempLabel->setText(DataList.at(i).name);
  38. QHBoxLayout* horizontalLayout = new QHBoxLayout;
  39. horizontalLayout->addWidget(DataCheckbox);
  40. horizontalLayout->addWidget(tempLabel);
  41. myTransducermodel->addColumn(DataList.at(i).name);
  42. verticalLayout[i]->addLayout(horizontalLayout);
  43. }
  44. verticalLayoutForWidget->addWidget(TypeGroupBox[i]);
  45. }
  46. }
  47. }
To copy to clipboard, switch view to plain text mode 

So I need to delete the layout and widgets, right?
AvailableDataWidget should be the parent of all layouts and widgets, so i tried this:
Qt Code:
  1. QList<QWidget *> widgets = ui->AvailableDataWidget->findChildren<QWidget *>();
  2. foreach(QWidget * widget, widgets){
  3. delete widget;
  4. }
To copy to clipboard, switch view to plain text mode 

but it makes my program crash.