How do you move a widget from one tab to another and save the layout

I have a tab widget and all tabs have the same layout, so I want to move the widget when the user changes tabs. What will be the code for this. I tried a few things, but it always seems like I'm missing something. The following works only once, but not from the slot called when the current tab changes: txDiag_1 is a custom widget that accepts the entire tab area tabList.at (i) is a link to the tab inside tabWidget and moveHlayout 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 need to remove the old widget first, but I decided that I could just destroy the old layout and create a new one every time, but it still didn't work.

+3
3

.

:

1. ,
2.
3. ( tabWidget, )

4. ,

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

P.S. , tabWidget , , , .

+1

, , , , , , ?

, QTabBar . currentChanged , , , .

+6

, . , .

, QWidget n QTabWidget .

, QTabWidget. , , . , QWidget.

enter image description here

enter image description here

5 :

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)));
}

, . , " ".

:

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);
}

. . . . . , .

When you are done, your single instance QWidgetwill be used in all tabs and it will resize / stretch accordingly due to its parent layout .

enter image description here

Many thanks.

+2
source

Source: https://habr.com/ru/post/1725456/


All Articles