Closing Tabs in Qt, multiple tabs close at once?

1k views Asked by At

So I've been about some sites for examples of code for a function to close tabs. I set the tabs to closable (that went absolutely fine), but the problem I have is that when I close tabs (specifically, the second tab onwards), it closes every tab after that one.

My header:

private slots:
void on_btn_newTab_clicked();
void on_tabWidget_tabCloseRequested(int index);

My cpp:

void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    connect(ui -> tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
}

void MainWindow::closeTab(int index)
{
    ui -> tabWidget -> removeTab(index);
}

Am I missing something? I assumed it was because I hadn't specified an index to close a specific tab, but then I thought the parameter for this slot was the index I needed. Am I wrong?

1

There are 1 answers

1
UldisK On BEST ANSWER

I dont think the connect part is right..

On on_tabWidget_tabCloseRequested(int index) you should call ui->tabWidget->removeTab(index) not connect a signal ..

I think that you create extra signals and thats why you remove all tabs after the tab you tried to close.

EDIT:

To expand a bit:

I think, on first click no tabs get deleted, but you create a connection to delete a tab with the on_tabWidget_tabCloseRequested(int index). On your second try you again, create a connection with on_tabWidget_tabCloseRequested(int index) and call closeTab. On your third try you create another connection and call closeTab twice with the same index, so the tab you clicked is deleted and the next one ...