How to focus on a new tab?

I managed to add a new tab using qtabwidget-> addTab (newtab, title);

But is it possible to focus on this in my code?

thank

+6
source share
4 answers

'setCurrentWidget' or 'setCurrentIndex' will complete the task.

You can use either a pointer to the added widget or a numerical index.

Cm:

http://doc.qt.io/qt-5/qtabwidget.html#setCurrentWidget

http://doc.qt.io/qt-5/qtabwidget.html#currentIndex-prop

, , 2- :

ui->tabWidget->setCurrentIndex(1);

(MyWidget QWidget), :

MyWidget* pointerToMyWidgetInTab = new MyWidget();
ui->tabWidget->addTab(pointerToMyWidgetInTab,"Tab2")
ui->tabWidget->setCurrentWidget(pointerToMyWidgetInTab2);
+2

Count the total number of tabs and set the last one:

ui->tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
+1
source

.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();
}
0
source

Source: https://habr.com/ru/post/1781096/


All Articles