Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 992
user360607 Avatar asked Nov 29 '11 09:11

user360607


People also ask

How do I use tab widget in Qt?

The normal way to use QTabWidget is to do the following: Create a QTabWidget. Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them. Insert child widgets into the page widget, using layouts to position them as normal.

How do I change my Qt tab name?

Select the parent QTabWidget in Object Inspector. First select the "currentIndex" corresponding to the tab you would like to alter and hit enter. Change the name using the currentTabText property. Continue doing this for all indexes 0-n.

How do I add a tab in PyQt?

PyQt tabs example To add a tab to a QTabWidget , call the method . addTab() . label1 = QLabel("Widget in Tab 1.") label2 = QLabel("Widget in Tab 2.")


3 Answers

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

    }
}
like image 158
thangdc94 Avatar answered Nov 12 '22 00:11

thangdc94


This is how I solved it

void MainWindow::on_tabWidget_currentChanged(int index)
{
    if (lockTabs) ui->tabWidget->setCurrentIndex(lockedTab);
}

On click of a button, I set lockTabs to true and save the current tab index to lockedTab (int). No matter what tab you click it will just throw you back to the locked tab.

I do agree with the first comment that disabling tabs is the better way tho. This is my solution for disabling tabs:

void MainWindow::lockTabs(int except){
    for (int i=0; i<ui->tabWidget->count(); i++) {
        if (i!=except) ui->tabWidget->setTabEnabled(i, false);
    }
}

void MainWindow::unlockTabs() {
    for (int i=0; i<ui->tabWidget->count(); i++) {
        ui->tabWidget->setTabEnabled(i, true);
    }
}
like image 20
cen Avatar answered Nov 11 '22 22:11

cen


In your header, declare:

QWidget *saveTab

Create a routine tabChanged have the slot for the currentChanged() signal. Then:

void pkgName::tabChanged
//"ask your question"
if "bad reply"
  // This is where you'll "set back to your old tab"
  ui->tabWidget->setCurrentWidget(savedWidget)
end if
savedWidget = ui->tabWidget-> getCurrentWidget() 
// Process
like image 1
WillW Avatar answered Nov 11 '22 22:11

WillW