How to focus on new tab?
I was able to add a new tab using qtabwidget->addTab ( newtab, title );
But is it possible to focus o开发者_开发百科n this tab in my code?
Thanks
'setCurrentWidget' or 'setCurrentIndex' will do the job.
You can use either the pointer to the added widget or a numeric index.
See:
http://doc.qt.io/qt-5/qtabwidget.html#setCurrentWidget
http://doc.qt.io/qt-5/qtabwidget.html#currentIndex-prop
For example, if you have a tab widget with say 3 tabs, you can focus on the 2nd tab like this:
ui->tabWidget->setCurrentIndex(1);
If you just want to use the pointer to your widget (MyWidget of type QWidget) then here is another example:
MyWidget* pointerToMyWidgetInTab = new MyWidget();
ui->tabWidget->addTab(pointerToMyWidgetInTab,"Tab2")
ui->tabWidget->setCurrentWidget(pointerToMyWidgetInTab2);
Count the total number of tabs and set the last one:
ui->tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
.h
private slots:
void setFocusAddedTab();
void on_addTabButton(); //Add Button
.cpp
void MainWindow::setFocusAddedTab() {
int x = ui->tabWidget->currentIndex()+1;
ui->tabWidget->setCurrentIndex(x)
}
void MainWindow::on_addTabButton() {
ui->tabWidget->addTab(...,...);
setFocusAddedTab();
}
精彩评论