PDA

View Full Version : QTabWidget not deleting the widget of a tab when this tab is closed



Matthiasabt
16th October 2013, 12:10
Hello,

I have a program whick has a TabWidget as the CentralWidget and i want the tabs to be closeable, but when I close a specific tab, the widget it contains is not deleted.
The TabWidget that I use is derived from the QTabWidget.
Maybe I must set the parent of the widget to be the tab, but how should i do that?

tabwidget.h

#ifndef TABWIDGET_H
#define TABWIDGET_H

#include <QTabWidget>

class TabWidget:public QTabWidget
{
Q_OBJECT
public:
TabWidget(QWidget *parent = 0);

public slots:
void closeTab(int index);
};

#endif // TABWIDGET_H


tabwidget.cpp

#include "tabwidget.h"
#include "settingstab.h"
#include <QDebug>

TabWidget::TabWidget(QWidget *parent):QTabWidget(parent) {
this->setParent(parent);
connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
}

void TabWidget::closeTab(int index) {
this->removeTab(index);
}


code where i add the tab


void MainWindow::addMyTab() {
tabwidget->addTab(new SettingsTab(), "Settings");
}

vilary
16th October 2013, 14:02
removeTab(index) - removes the tab at position index from this stack of widgets. The page widget itself is not deleted.
You can remove your widget using function QWidget * QTabWidget::widget(int index) const