Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved How to add dynamically tabs to QTabWidget created by QT Creator

    General and Desktop
    2
    4
    2627
    Loading More Posts
    • 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.
    • ?
      A Former User last edited by

      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)
      
      mrjj 1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion @Guest last edited by

        @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 Reply Quote 2
        • ?
          A Former User last edited by

          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 Reply Quote 1
          • mrjj
            mrjj Lifetime Qt Champion last edited by

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post