PDA

View Full Version : adding and removing widgets



eric
29th November 2007, 17:56
Hello!
My program adds widgets (QPushButtons) like this:

Layout->addWidget(MyWidget);
and removes them like this:

Layout->removeWidget(MyWidget);
several times during the code execution.

Although this simple approach works most of the time, sometimes I get weird results. Sometimes after removing my QPushButton, it no loger functions but it is still there and sitting on top of another widget that is supposed to replace it.
Questions:
1. Do you have to check for the presence of a widget before you attempt to remove it? How do you do it?
2. Does something need to be specifically refreshed or redrawn after the widget has been removed?
Thank you!

high_flyer
30th November 2007, 02:34
Its hard with out knowing the inside of your code better, but here what I think:

Sometimes after removing my QPushButton, it no loger functions but it is still there and sitting on top of another widget that is supposed to replace it.


void QLayout::removeWidget ( QWidget * widget )

Removes the widget widget from the layout. After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout.

Note: The ownership of widget remains the same as when it was added.

And when you add a widget:

void QLayout::addItem ( QLayoutItem * item ) [pure virtual]

Implemented in subclasses to add an item. How it is added is specific to each subclass.

This function is not usually called in application code. To add a widget to a layout, use the addWidget() function; to add a child layout, use the addLayout() function provided by the relevant QLayout subclass.

Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.

you need to explicitly delete the widget after removing it - or place it somewhere else.