how to restore the removed tab from central Widget of QMainWindow

  • I have following code
    d_tab = new QTabWidget
    void MyMainWindow::createTabWidget(Qt::DockWidgetArea corner)
    myView = new MyView(NULL);
    addTab(myView, tr("&MyView"));

    void MyMainWindow::closeTab()
    if (QWidget* w = d_tab->widget(index)) {
    QString title = d_tab->tabText(index);
    bool enabled = d_tab->isTabEnabled(index);

    GQDockWidget* dw = newDockWidget(w, title, enabled);

    QObject::connect(d_tab, SIGNAL(closeTab(int)), this, SLOT(closeTab(int)));
    Using code I want to restore the closed tab

  • Qt Champions 2018

    @Qt-Enthusiast Then create add add the tab again

  • Moderators

    @Qt-Enthusiast removeTab does not delete the widget so you can easily add it back later. The only thing you need is probably a pointer to your widget. You should do that anyway, because with the code you have right now it is just floating in your memory unused, after it has been removed.

    so create a class-member:

    QWidget *removedTab = Q_NULLPTR;

    before you remove the tab set the pointer

    removedTab = d_tab->widget(index);

    to add it back again

        d_tab->insertTab(index,removedTab, const QString &label);

  • ut is there any automatic way to do the same
    any signal
    that does the same job I just emit that signal instead of calling the insert tab code

  • Moderators

    @Qt-Enthusiast it doesn't get much easier than that, but If you dont want to handle this in your main class, you can derive your own tabwidget class and create such a function.

    #include <QTabWidget>
    class myTabWidget : public QTabWidget{
        explicit myTabWidget(QWidget * parent = 0) : QTabWidget(parent){}
        void removeTab(int index){
          lastRemoved = widget(index);
          m_index = index;
          m_TabTitel = tabText(index);
       void restoreTab(){
               insertTab(m_index, lastRemoved, m_TabTitel )
               lastRemoved = Q_NULLPTR;
       QWidget *lastRemoved = Q_NULLPTR;
       int m_index = 0;
       QString m_TabTitel;

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.