Hello,
I created a scroll area and called setWidget() to set the "inner" widget. The inner widget has a layout that I want to clear from time to time. In the documentation for QLayout::takeAt() I found this:

http://doc.trolltech.com/4.5/qlayout.html#takeAt

Unfortunately, it does not work (it looks like a redraw problem). What works is keeping a separate list of added widgets, and deleting them manually. I can't understand what's going on. I'm posting a complete test class showing the problem:

Qt Code:
  1. #include <QWidget>
  2. #include <QVBoxLayout>
  3. #include <QPushButton>
  4. #include <QScrollArea>
  5. #include <QLabel>
  6. #include <QList>
  7.  
  8. class ScrollAreaTest : public QWidget
  9. {
  10. Q_OBJECT
  11.  
  12. public:
  13. ScrollAreaTest(QWidget* parent = 0):
  14. QWidget(parent)
  15. {
  16. // buttons
  17. QPushButton* btnAdd = new QPushButton("Add label");
  18. connect(btnAdd, SIGNAL(clicked()), this, SLOT(onBtnAdd()));
  19.  
  20. QPushButton* btnClear1 = new QPushButton("Clear Layout (mode 1)");
  21. connect(btnClear1, SIGNAL(clicked()), this, SLOT(onBtnClear1()));
  22.  
  23. QPushButton* btnClear2 = new QPushButton("Clear Layout (mode 2)");
  24. connect(btnClear2, SIGNAL(clicked()), this, SLOT(onBtnClear2()));
  25.  
  26. // scroll area
  27. QScrollArea* myScrollArea = new QScrollArea();
  28. myScrollArea->setWidgetResizable(true);
  29. myScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  30. myScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
  31.  
  32. // scroll area "inner" widget
  33. QWidget* innerWidget = new QWidget();
  34. innerWidgetLayout = new QVBoxLayout();
  35. innerWidget->setLayout(innerWidgetLayout);
  36.  
  37. // set scroll area inner widget
  38. myScrollArea->setWidget(innerWidget);
  39.  
  40. // main layout
  41. QVBoxLayout* layout = new QVBoxLayout();
  42. layout->addWidget(btnAdd);
  43. layout->addWidget(btnClear1);
  44. layout->addWidget(btnClear2);
  45. layout->addWidget(myScrollArea);
  46. setLayout(layout);
  47. }
  48.  
  49. private:
  50. QVBoxLayout* innerWidgetLayout;
  51. QList<QLabel*> addedLabels;
  52.  
  53. private slots:
  54.  
  55. void onBtnAdd()
  56. {
  57. QLabel* l = new QLabel("Hello there!");
  58. innerWidgetLayout->addWidget(l);
  59.  
  60. addedLabels.push_back(l); // I'm keeping a list of added labels (see onBtnClear2)
  61. }
  62.  
  63. void onBtnClear1() // this is from the docs and doesn't work
  64. {
  65. QLayoutItem *child;
  66.  
  67. while ((child = innerWidgetLayout->takeAt(0)) != 0)
  68. delete child;
  69.  
  70. /*
  71. innerWidgetLayout->activate();
  72. innerWidgetLayout->update();
  73. update();
  74. */
  75. }
  76.  
  77. void onBtnClear2() // this works
  78. {
  79. for (QList<QLabel*>::iterator it = addedLabels.begin(); it != addedLabels.end(); ++it)
  80. delete *it;
  81.  
  82. addedLabels.clear();
  83. }
  84. };
To copy to clipboard, switch view to plain text mode 

Thank you