Results 1 to 2 of 2

Thread: QScrollArea: clearing the layout of the inner widget

  1. #1
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QScrollArea: clearing the layout of the inner widget

    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

  2. #2
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea: clearing the layout of the inner widget

    I solved changing

    Qt Code:
    1. QLayoutItem *child;
    2. while ((child = layout->takeAt(0)) != 0) {
    3. ...
    4. delete child;
    5. }
    To copy to clipboard, switch view to plain text mode 

    to

    Qt Code:
    1. QLayoutItem *child;
    2. while ((child = layout->takeAt(0)) != 0) {
    3. ...
    4. delete child->widget(); // delete Layout Item's underlying widget
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTreeWidgetItem - widget with layout collapsing
    By enkidu in forum Qt Programming
    Replies: 4
    Last Post: 9th August 2009, 22:50
  2. changing layout of a widget
    By mikro in forum Qt Programming
    Replies: 10
    Last Post: 4th August 2009, 20:21
  3. hiding a widget without rearrange layout
    By mastupristi in forum Newbie
    Replies: 1
    Last Post: 6th July 2009, 17:20
  4. Resize widget force layout resizing
    By ^NyAw^ in forum Qt Programming
    Replies: 17
    Last Post: 11th February 2009, 11:27
  5. resizing events of a custom widget in a layout
    By Rooster in forum Qt Programming
    Replies: 7
    Last Post: 16th February 2008, 10:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.