How to identify when the current tab is changing in a QTabWidget?

user360607 picture user360607 · Nov 29, 2011 · Viewed 21.5k times · Source

I'm using a QTabWidget and I need a way to handle the change of the current tab before it actually happens, and possibly cancel it if certain conditions are met. The QTabWidget::currentChanged signal is received after the current tab has changed, but is there a QTabWidget::currentChanging signal or another way to achieve the behavior I need?

Answer

thangdc94 picture thangdc94 · Aug 28, 2015

In my case, I connect SIGNAL and SLOT like this:

//check if user clicked at a tab
connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(tabSelected()));

and in tabSelected() function, I check current Tab Index:

void MainWindow::tabSelected(){
    if(ui->tabWidget->currentIndex()==0){

        // Do something here when user clicked at tab1

    }
    if(ui->tabWidget->currentIndex()==3){

        // Do something here when user clicked at tab4

    }
}