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.开发者_Python百科 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.
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.
Yan's answer is good, but I found it difficult to understand. I hope this answer will elaborate upon the solution.
The goal is to share a single instance of a QWidget
across n
distinct QTabWidget
pages.
Each page will hold one widget that takes all the available area in the QTabWidget
. If you want multiple user interface elements on a single page, put them in a single widget like I have done below. So, create your custom QWidget
. Here is mine
Here is where I dynamically create 5 tabs:
void MyWindow::setupTabs()
{
for (int i = 0; i < 5; ++i)
{
QWidget * w = new QWidget;
w->setLayout(new QHBoxLayout);
ui->tabWidget->addTab(w, "Tab " + std::to_string(i));
}
connect(
ui->tabWidget,
SIGNAL(currentChanged(int)),
this,
SLOT(onTabChanged(int)));
}
Set a layout for each tab when you create them, as seen above. This maintains the invariant that "every page widget has a layout at all times".
Here is the slot:
void MyWindow::onTabChanged(int index)
{
QLayout * layout = ui->tabWidget->widget(index)->layout();
layout->removeWidget(ui->tabWidget->widget(index));
delete layout;
layout = new QHBoxLayout;
layout->addWidget(ui->flowTabWidget);
ui->tabWidget->widget(index)->setLayout(layout);
}
In the slot we get the layout of the page widget. Then we remove the page widget from its own layout. Next we delete that layout. We create a new layout. We add our custom widget to the layout. Finally, we set the layout on the page widget.
When you are finished, your single QWidget
instance will be used across all tabs and it will resize/stretch appropriately due to its parent's layout.
Now thank you very much.
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.
setParent is enough to move a widget from one container to another one. but when you change the parent, the widget is hidden, so, you need to show it again.
ui.txDiag_1->setParent(tabList.at(1));
ui.txDiag_1->show();
精彩评论