Qtabwidget change widget of existing tab
-
Hello again,
Is there a function to change the widget of an existing tab in qtabwidget ? In designer, dropping a Tab Widget in the window creates by default 2 tabs. I can drag and drop widgets in each tab, but I want to do it dynamically, from code. I'm looking for something like tabWidget->setWidgetAt(index)=...I know I can achieve this by adding new tabs, but I'd like to know if it's possible to edit widgets of the tabs created in the designer.
-
Hello again,
Is there a function to change the widget of an existing tab in qtabwidget ? In designer, dropping a Tab Widget in the window creates by default 2 tabs. I can drag and drop widgets in each tab, but I want to do it dynamically, from code. I'm looking for something like tabWidget->setWidgetAt(index)=...I know I can achieve this by adding new tabs, but I'd like to know if it's possible to edit widgets of the tabs created in the designer.
@cpper Wll, of course,
with
QWidget *QTabWidget::widget(int index)
You get a pointer the the widget, with that you can do what ever you want.
-
-
Actually the error makes sense, the return type must have been reference to pointer in order for the code to work.
This makes more sense, but it also doesn't work, because the = operator of QWidget is private.*ui->tabWidget->widget(0)=QPushButton();
@cpper Ok sry, I know get what you try to do.
You want to replace a QWidget of your QTabwidget with another one. I'm afraid there is no way around an a
QTabWidget::insertTab
andQTabWidget::removeTab
combinationbut you could easily subclass QTabWidget and make such a function.
//Untested public slots: void replaceTab(int index, QWidget *page, QString title){ if(title.isEmpty()) title = tabText(index); auto toDelete = widget(index); removeTab(index); toDelete->deleteLater(); insertTab(index, page, title); }