Take the 2-minute tour ×

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?

share|improve this question
I don't believe there is such a hook...you would have to manually flip back. But this doesn't sound like a very good user interface choice. Why wouldn't you make the tab's enablement state reflect its availability instead of trying to "reject" a click on a valid-looking tab? doc.qt.nokia.com/latest/qtabwidget.html#setTabEnabled –  HostileFork Nov 29 '11 at 9:49
Would it be possible to subclass QTabWidget? I haven't done any research on the matter; it's just an idea. –  Chris Parton Nov 29 '11 at 9:55
We had exactly this problem and eventually chose to write a custom TabWidget from scratch which produced an about-to-change signal and allowed objects to veto proposed changes. Having said that I would have chosen to use the method proposed by @HostileFork if that had been an option. –  sjwarner Nov 29 '11 at 9:56
@sjwarner Any solution that makes the user feel like they're in control is better than one where it feels like it is broken or failing to act like they would expect. Another choice would be that instead of flipping back and "undoing" their seemingly-legitimate tab click, one could use something like a QStackedWidget on the target tab. It could by default have a widget in it that said "nothing to see here unless you go back to some other tabs". But if the checks passed it would show the "real" tab page contents. doc.qt.nokia.com/latest/qstackedwidget.html#details –  HostileFork Nov 29 '11 at 11:04
I have successfully achieved such a behaviour by inheriting QTabWidget. As I do not have access to the code now, I'll wait until I go home to post my code if it's of any interest. –  ixM Nov 29 '11 at 12:17
show 2 more comments

1 Answer

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() {
        ui->tabWidget->setTabEnabled(i, true);
share|improve this answer
add comment

Your Answer


