Qtabwidget change widget of existing tab
-
wrote on 26 Apr 2017, 09:24 last edited by
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.
-
wrote on 26 Apr 2017, 09:39 last edited by
Thanks for the quick reply.
I already tried that, but strangely got a lvalue error, so I though that's now the right way to do it:ui->tabWidget->widget(0)=new QPushButton();
error: C2106: '=': left operand must be l-value
-
wrote on 26 Apr 2017, 10:23 last edited by cpper
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();
-
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); }
1/5