Posted by Kieren Searle on the 08/25/11

Dynamically removing QWidgets from a QLayout

I’ve seen some strange things when trying to remove QWidgets from a QLayout while using code such as:

Code:

MyWidget->layout()->removeWidget(AnotherWidget);

Sometimes AnotherWidget wouldn’t be removed, sometimes it would display only part of it.

After a bit of searching I came across a different solution that worked a treat. The below code removes all widgets MyWidget’s layout.

Code:

while((QLayoutItem *child = MyWidget->layout()->takeAt(0)) != 0)
{
    delete child->widget();
    delete child;
}

And if you wanted to only remove a single widget from the layout:

Code:

while((QLayoutItem *child = MyWidget->layout()->takeAt(0)) != 0)
{
    if(child == AnotherWidget)
    {
        delete child->widget();
        delete child;
    }
}

Source: http://www.qtcentre.org/archive/index.php/t-21561.html

Trackback address for this post

This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

Array

Feedback awaiting moderation

This post has 1 feedback awaiting moderation...

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

Array

My name is Kieren and this is my blog. I use it mostly to document any solutions to problems I come across in computing.

Search

XML Feeds