How to add dynamically tabs to QTabWidget created by QT Creator



  • I am trying to add dynamically tabs to QTabWidget created by QT Creator. My problem is when I create the interface with QT Creator, there are 2 tabs created by default : Tab 1 and Tab 2. To add tabs, I used the method insertTab.

    To remove Tab 1 and Tab 2, I used the method remove tabs.

    I am searching for a new method that avoids removing tab 1 and tab 2 and put tabs in the corect order.

    I put my code :

    ```
    
    self.setupUi(self)
    
        self.tabs = self.findChild(QtGui.QTabWidget, 'tabWidget')
    
        checked_list = [u'Base Scenario 1991 - 91A', u'Intermediate 1996 - 96A', u'Trend Scenario 2001 - 01T']
        nb_tabs = len(checked_list)
    
        for i in range(nb_tabs) :
            self.add_new_tab(i,checked_list[i])
    
    
        self.remove_default_tabs(nb_tabs)
        self.tabs.addTab(QtGui.QWidget(),"All checked scenarios")
    
    
    def add_new_tab(self,index,text):
    
        self.new_tab = QtGui.QWidget()
        self.tabs.insertTab(index,self.new_tab,text)
        self.tabs.setTabText(index,text)
    
    def remove_default_tabs(self,nb_tabs):
         # we suppose that in QT Creator we don't change the names of the default tabs (Tab 1 and Tab 2)
        if self.tabs.tabText(nb_tabs) == "Tab 1" :
            self.tabs.removeTab(nb_tabs)
    
        if self.tabs.tabText(nb_tabs) == "Tab 2" :
            self.tabs.removeTab(nb_tabs)
    

  • Qt Champions 2016

    @EJWA
    Hi
    There is
    http://doc.qt.io/qt-5/qtabwidget.html#clear
    It will clear the tabwidget but NOT delete the pages.( the widgets)
    There are still in self.ui.NAME
    You can then insert again if its not right order to begin with.

    Im not sure what function u are looking for ?
    Something that could re-arrange the index or ?



  • Hello,

    Thank you mrjj for your answer.

    Finally, I found an easy solution which consists in deleting default tabs (Tab 1 and Tab 2) from QT Creator. It is very easy and simple.

    Thanks again.


  • Qt Champions 2016

    Super
    Hehe
    I didn't suggest that as i assumed you wanted to keep them :)


Log in to reply
 

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