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"));
    setCentralWidget(d_tab);
    }

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

    w->setParent(NULL);
    GQDockWidget* dw = newDockWidget(w, title, enabled);
    dw->setTabified(true);
    

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


  • Moderators

    @Qt-Enthusiast Then create add add the tab again



  • @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:

    private:
    QWidget *removedTab = Q_NULLPTR;
    

    before you remove the tab set the pointer

    removedTab = d_tab->widget(index);
    d_tab->removeTab(index);
    

    to add it back again

    if(removedTab)
        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



  • @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{
        Q_OBJECT
        explicit myTabWidget(QWidget * parent = 0) : QTabWidget(parent){}
        ~myTabWidget(){if(lastRemoved)lastRemoved->deleteLater();}
    
    public:
        void removeTab(int index){
          lastRemoved = widget(index);
          m_index = index;
          m_TabTitel = tabText(index);
          QTabWidget::removeTab(index);
        }
    
       void restoreTab(){
            if(lastRemoved){
               insertTab(m_index, lastRemoved, m_TabTitel )
               lastRemoved = Q_NULLPTR;
            }
       }
    
    private:
       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.