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 add dynamically tabs to QTabWidget created by QT Creator

How to add dynamically tabs to QTabWidget created by QT Creator

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 3.8k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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)
    
    mrjjM 1 Reply Last reply
    0
    • ? A Former User

      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)
      
      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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 ?

      1 Reply Last reply
      2
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

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