Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to restore the removed tab from central Widget of QMainWindow
QtWS25 Last Chance

how to restore the removed tab from central Widget of QMainWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 930 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by Qt Enthusiast
    #1

    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

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • Q Qt Enthusiast

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Qt-Enthusiast Then create add add the tab again

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Q Qt Enthusiast

        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

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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);
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        2
        • Q Offline
          Q Offline
          Qt Enthusiast
          wrote on last edited by
          #4

          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

          J.HilkJ 1 Reply Last reply
          0
          • Q Qt Enthusiast

            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

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @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;
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved