首先,在您的小部件中添加一个空选项卡,然后连接currentChanged
信号:
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();
}
然后,在您的onChangeTab
插槽上,检查用户是否单击了最后一个选项卡,然后调用newTab
:
void TabsView::onChangeTab(int index)
{
if (index == this->ui->tabWidget->count() - 1) {
newTab();
}
}
最后,在您的newTab
方法上,创建新选项卡并选择它:
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);
}