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?
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.
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.
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.")
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
}
}
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);
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With