In this example the text of the labels are re-drawn and overlay each other when pressing the button. Is there a possibility to clear the layout mGridLayout before calling Initialize()? Or is the only solution to save the labels globally and simply set the text instead of creating them each time?

Qt Code:
  1. MyDialog::MyDialog(QWidget *parent, Qt::WFlags flags)
  2. : QDialog(parent, flags)
  3. {
  4. // test input
  5. mTermName.append("term 1");
  6. mTermName.append("term 2");
  7. mValues.append(1.23);
  8. mValues.append(3.4322);
  9.  
  10. QGridLayout *mMainLayout = new QGridLayout(this);
  11. mGridLayout = new QGridLayout;
  12. Initialize(); // initializes mGridLayout with its content
  13. mMainLayout->addLayout(mGridLayout, 0, 0);
  14.  
  15. QPushButton *btn = new QPushButton("press me");
  16. Q_ASSERT(connect(btn, SIGNAL(clicked()), this, SLOT(slotPressed())));
  17. mMainLayout->addWidget(btn, 1, 0);
  18.  
  19. setLayout(mMainLayout);
  20. }
  21.  
  22.  
  23. void MyDialog::slotPressed()
  24. {
  25. mValues[0] = 2.5;
  26. Initialize();
  27. }
  28.  
  29. void MyDialog::Initialize()
  30. {
  31. int nbrTerms = 2;
  32. int row = 1;
  33. for(int i=0; i<nbrTerms; i++)
  34. {
  35. QString termName = QString("%1").arg(mTermName[i]);
  36. double termValue = mValues[i];
  37.  
  38. QLabel *nameLabel = new QLabel(termName);
  39. QString parameterValueText = QString("%1").arg( termValue, 1, 'f', 3 );
  40.  
  41. QLabel *termValueLabel = new QLabel(parameterValueText);
  42. mGridLayout->addWidget(nameLabel, row, 1);
  43. mGridLayout->addWidget(termValueLabel,row, 2);
  44. row++;
  45. }
  46. }
To copy to clipboard, switch view to plain text mode