qt-interest@trolltech.com
[Top] [All Lists]

Re: delete a widget

Subject: Re: delete a widget
From: Kuba Ober
Date: Fri, 25 Mar 2005 09:52:07 -0500
> void QLayout::removeItem ( QLayoutItem * item )
> 
> Removes the layout item item from the layout. It is the caller's
> responsibility to delete the item.

Given that you'd have to get pointer to the QLayoutItem first, it's a really 
convoluted way to do it. In general case of arbitrary layout, the only way 
you can get pointer to a QLayoutItem is via the iterator. So you're looking 
at code that looks like this:

//
QLayout * layout = ...; // points to the layout
QWidget * widget = ...; // points to the widget to be removed
QLayoutIterator it = layout->iterator();
while (it.current()) {
        if (it.current()->widget() == widget) {
                layout->removeItem(it.current());
                break;
        }
        ++ it;
}
delete widget;
//

Now, since just deleting the widget will make sure that relevant layout items 
are removed *anyway*, the correct way to do all this is:

delete widget;

Just in case anyone wondered, here is a snippet from QLayout::eventFilter:

...
    case QEvent::ChildRemoved:
        {
            QChildEvent *c = (QChildEvent *)e;
            if ( c->child()->isWidgetType() ) {
                QWidget *w = (QWidget *)c->child();
                if ( removeWidgetRecursively( this, w ) ) {
                    QEvent *lh = new QEvent( QEvent::LayoutHint );
                    QApplication::postEvent( o, lh );
                }
            }
        }

I hope that reinserting a widget at given row/column of a QGridLayout is a no 
brainer *after* one reads QGridLayout's fine documentation. Hint: look for 
methods other than addItem.

Cheers, Kuba

--
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>