tags:

views:

72

answers:

2

I have a tabbed widget and all tabs have the same layout so I want to move the widget when the user changes tabs. What would be the code for this. I have tried a few things but it always seems to be missing one thing. the following only worked once but not from the slot called when the current tab is changed: txDiag_1 is a custom widget taking the whole tab area tabList.at(i) is a reference to the tab inside the tabWidget and movingHlayout is a horizontal layout.

ui.txDiag_1->setParent(tabList.at(1));
movingHlayout->setParent(tabList.at(1));
movingHlayout->setSpacing(3);
movingHlayout->setMargin(3);
movingHlayout->setObjectName(QString::fromUtf8("movingHlayout"));
movingHlayout->addWidget(ui.txDiag_1);
tabList.at(1)->setLayout(movingHlayout);

I thought maybe I should remove the old widget first but I figured I could just destroy the old layout and create a new one each time but still it didnt work.

+5  A: 

Just to clarify, you have a set of tabs that control a widget which changes slightly when the user changes tabs, and you want to reuse that widget instead of creating one for each tab?

If that is the case, then add a QTabBar and have a single widget. Then connect the currentChanged signal on the tabbar to your own slot, so when the user changes the current tab, you update the widget.

Marius
ok thanks I will try that. sorry for the long response, had a long week end :)
yan bellavance
A: 

Ok got it.

basically you need to:

1.remove the widget you want to move from the layout
2. delete the layout
3.Create a new layout using the constructor with parent parameter (parent being one of the tabs of the tabWidget you wish to move your widget to)
4.Add the widget you wish to move to the layout

layoutPointer->removeWidget(ui.WidgetName);
delete layoutPointer;
layoutPointer= new QHBoxLayout(destTabName);
layoutPointer->addWidget(WidgetName);

P.S. Make sure you have only one layout in your tabWidget and use its pointer which you delete and recreate the layout its pointing to or else it wont work.

yan bellavance