First, add an empty tab to your widget, and connect the currentChanged
signal:
TabsView::TabsView(QWidget *parent) :
QWidget(parent),
ui(new Ui::TabsView)
{
ui->setupUi(this);
ui->tabWidget->clear();
ui->tabWidget->addTab(new QLabel("+"), QString("+"));
connect(ui->tabWidget, &QTabWidget::currentChanged, this, &TabsView::onChangeTab);
newTab();
}
Then, on your onChangeTab
slot, check if the user is clicking on the last tab, and call newTab
:
void TabsView::onChangeTab(int index)
{
if (index == this->ui->tabWidget->count() - 1) {
newTab();
}
}
Finally, on your newTab
method, create the new tab and select it:
void TabsView::newTab()
{
int position = ui->tabWidget->count() - 1;
ui->tabWidget->insertTab(position, new QLabel("Your new tab here"), QString("New tab"));
ui->tabWidget->setCurrentIndex(position);
auto tabBar = ui->tabWidget->tabBar();
tabBar->scroll(tabBar->width(), 0);
}
QMdiArea
, butQTabWidget
has aQTabBar
which has a setTabButton() function. Perhaps you should check that out? – Despumate