Important: Please read the Qt Code of Conduct -

QTabWidget: tabCloseRequested(int) not emitted

  • According to the documentation:

    This signal is emitted when the close button on a tab is clicked

    in my application (Qt 5.11.0) I placed a QTabWidget in my QMainWindow using the designer. Then I set the tabClosable property to true. In my code I created this slot:

    void MainWindow::on_tabView_tabCloseRequested(int index)
        qDebug() << "close requested" << index;

    tabView is the name of the QTabWidget. The tabs are added dynamically in this way:

    void MainWindow::createViewTabs(QStringList names)
        foreach (QString name, names)
            QWidget *widget = new QWidget(ui->tabView);
            widget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);        
            ui->tabView->addTab(widget, name);

    but when I click the close button the signal is not emitted - or at least the slot is not executed. I don't have any warning about wrong signal-slot connection.

    Do you see any evidence of errors here?

  • Lifetime Qt Champion

    @Mark81 Did you try to connect the signal to your slot manually to see whether it makes a difference?

  • @jsulm good catch. Using:

    connect(ui->tabView, &QTabWidget::tabCloseRequested, this, &MainWindow::tabView_tabCloseRequested);

    it works.
    But I dont't understand what's wrong. I created the implicit slot right-clicking on the QTabWidget in the designer and selecting "go to slot". As said, there were no warnings about any syntax error in the connection.

  • Lifetime Qt Champion

    @Mark81 said in QTabWidget: tabCloseRequested(int) not emitted:

    right-clicking on the QTabWidget in the designer and selecting "go to slot"

    I did the same and it works, but I did not add tabs manually.

Log in to reply