PDA

View Full Version : replace a widget in QVBoxLayout



migel
31st May 2011, 19:07
I would love to replace a widget on button action a widget which is in the middle.

layout = new QVBoxLayout;
layout->addWidget(_top);
layout->addWidget(_middle);
layout->addWidget(_bottom);

now I can remove a widget using

layout->removeWidget(_middle);

but how to add another one on that spot ??

Thanks for help.

stampede
31st May 2011, 19:11
Maybe try inserting the widget at correct index with insertWidget (http://doc.qt.nokia.com/latest/qboxlayout.html#insertWidget) ?

migel
31st May 2011, 19:26
heh, yeah that works :) thanks

but removeWidget seems to not work

layout->removeWidget(_middle1);
layout->insertWidget(1,_middle0);

this looks like it inserts _middle0 next to _middle1, very weird

stampede
31st May 2011, 19:39
but removeWidget seems to not work
Are you sure the widget you want to remove is in the layout ? What if you call hide() on this widget ?
As for your problem, you can always add all of your widgets to a layout at once in desired order, and then call show() and hide() on correct widgets. If there are only three widgets visible, with top and bottom widget constantly occupying the same place in layout, it should work.

migel
31st May 2011, 22:01
Thanks hide and show worked very well, thank you. Wonder what I am doing wrong with adding/removing widget. Hope it won't effect anything.