Varun Chitre
Varun Chitre

Reputation: 3220

Opening closable tabs using push button in Qt

I am making a GUI program in Qt.

I have one tab and in that tab there is push button, what I want is when that button is clicked then it should open a tab in the same widget and it should be closable. How can I do that? A example code will be good.

Thank you

Upvotes: 0

Views: 1331

Answers (1)

bjoernz
bjoernz

Reputation: 3852

I do not fully understand the question, but here is my best attempt at answering it. I suppose that you use QTabWidget for your tabs. You can add a widget and select it by the following code:

QWidget* widget;
int index = ui->tabWidget->addTab(widget, "Description");
ui->tabWidget->setCurrentIndex(index);

You cannot make single tabs closable, but you can intercept the close event:

ui->tabWidget->setTabsClosable(true);
connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));

And handle unwanted close events like this:

void MainWindow::closeTab(int index)
{
    if (index == 0)
    {
        return; // don't close the first tab
    }
    QWidget* widget = ui->tabWidget->widget(index);
    ui->tabWidget->removeTab(index);
    delete widget;
}

EDIT:

You can insert Tabs with:

ui->tabWidget->insertTab(index, widget, "Description");

Here is the documentation for the QTabWidget class.

Upvotes: 2

Related Questions