Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to add my UI in new tab to qtabwidget?
Forum Update on Monday, May 27th 2025

How to add my UI in new tab to qtabwidget?

Scheduled Pinned Locked Moved Unsolved Qt for Python
10 Posts 2 Posters 1.8k 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.
  • A Offline
    A Offline
    aadil50
    wrote on last edited by aadil50
    #1
    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets,uic
    
    class TestGUI(QtWidgets.QMainWindow):
        def __init__(self):
            super(TestGUI, self).__init__()
            self.myui =  uic.loadUi('SPTMain.ui', self)
            self.NewTab.clicked.connect(self.handleAddTab)
    
        def handleAddTab(self):
            contents = QtWidgets.QWidget(self.myui)
            layout = QtWidgets.QVBoxLayout(contents)
            self.tabWidget.addTab(contents, 'Tab One')
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        window = TestGUI()
        window.show()
        sys.exit(app.exec_())
    

    Actually It adds blank tab... Kindly guide me how can I add new tab as my ui (duplicate of my .ui file in new tab)

    JonBJ 1 Reply Last reply
    0
    • A aadil50
      import sys
      from PyQt5 import QtCore, QtGui, QtWidgets,uic
      
      class TestGUI(QtWidgets.QMainWindow):
          def __init__(self):
              super(TestGUI, self).__init__()
              self.myui =  uic.loadUi('SPTMain.ui', self)
              self.NewTab.clicked.connect(self.handleAddTab)
      
          def handleAddTab(self):
              contents = QtWidgets.QWidget(self.myui)
              layout = QtWidgets.QVBoxLayout(contents)
              self.tabWidget.addTab(contents, 'Tab One')
      
      if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          window = TestGUI()
          window.show()
          sys.exit(app.exec_())
      

      Actually It adds blank tab... Kindly guide me how can I add new tab as my ui (duplicate of my .ui file in new tab)

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @aadil50 said in How to add my UI in new tab to qtabwidget?:

      contents = QtWidgets.QWidget(self.myui)

      What is this supposed to do? It creates a new, empty QWidget with self.myui as its parent, hence a "blank tab".

      I don't understand the architecture of your application. It has a QMainWindow, which is what you show(). Meanwhile that main window creates some self.myui with SPTMain.ui loaded into itself. Yet at the same time you want it loaded into some other widget to use for tab within the main window.

      1 Reply Last reply
      2
      • A Offline
        A Offline
        aadil50
        wrote on last edited by
        #3

        So how can I improve it to achieve my task.. As you know i have shown what I want to do and what i have done.. Thanks...

        JonBJ 1 Reply Last reply
        0
        • A aadil50

          So how can I improve it to achieve my task.. As you know i have shown what I want to do and what i have done.. Thanks...

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @aadil50 said in How to add my UI in new tab to qtabwidget?:

          As you know i have shown what I want to do

          Maybe someone else understands what your SPTMain.ui is, whether it is the content for the main window or for some widget you want to add as a tab to somewhere, or what, and what your variable named self.myui is for --- but I don't. So hopefully someone else will answer you.

          [Maybe you should have no contents = QtWidgets.QWidget(self.myui), self.myui should be renamed self.tab1 and just self.tabWidget.addTab(self.tab1, 'Tab One'). I don't know.]

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aadil50
            wrote on last edited by
            #5

            @aadil50 said in How to add my UI in new tab to qtabwidget?:

            self.myui

            It is my ui which contain multliple Widgets.. I just want to show it in my tab.. By inheriting my class of ui and adding to addtab funtion hangs and windows disappear.. So I have done all my tries what i can and know.. But still cannot get idea how can i show my ui in new tab aka duplicate my ui in new tab...

            JonBJ A 2 Replies Last reply
            0
            • A aadil50

              @aadil50 said in How to add my UI in new tab to qtabwidget?:

              self.myui

              It is my ui which contain multliple Widgets.. I just want to show it in my tab.. By inheriting my class of ui and adding to addtab funtion hangs and windows disappear.. So I have done all my tries what i can and know.. But still cannot get idea how can i show my ui in new tab aka duplicate my ui in new tab...

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @aadil50
              So did you try what I suggested?

              1 Reply Last reply
              0
              • A aadil50

                @aadil50 said in How to add my UI in new tab to qtabwidget?:

                self.myui

                It is my ui which contain multliple Widgets.. I just want to show it in my tab.. By inheriting my class of ui and adding to addtab funtion hangs and windows disappear.. So I have done all my tries what i can and know.. But still cannot get idea how can i show my ui in new tab aka duplicate my ui in new tab...

                A Offline
                A Offline
                aadil50
                wrote on last edited by
                #7

                @aadil50 said in How to add my UI in new tab to qtabwidget?:

                By inheriting my class of ui and adding to addtab funtion hangs and windows disappear..

                So did you try what I suggested?

                Yes....
                This happens.. Kindly can you please explain your code.. maybe i am interpreting it wrong.. Or should i try something else you will suggest..

                [I am new user so cannot post reply under 600 seconds aka 10 minutes.. No reputation yet ]

                JonBJ 1 Reply Last reply
                0
                • A aadil50

                  @aadil50 said in How to add my UI in new tab to qtabwidget?:

                  By inheriting my class of ui and adding to addtab funtion hangs and windows disappear..

                  So did you try what I suggested?

                  Yes....
                  This happens.. Kindly can you please explain your code.. maybe i am interpreting it wrong.. Or should i try something else you will suggest..

                  [I am new user so cannot post reply under 600 seconds aka 10 minutes.. No reputation yet ]

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8

                  @aadil50 said in How to add my UI in new tab to qtabwidget?:

                  Kindly can you please explain your code

                  • contents = QtWidgets.QWidget(self.myui) You do not want this, I explained what it does earlier.
                  • self.myui should be renamed self.tab1 The .ui file seems to describe the content of a tab, so it's a name which means something.
                  • self.tabWidget.addTab(self.tab1, 'Tab One') Intended to add self.tab1 as a tab.

                  I'm a little confused by two different patterns I see for uic.loadUi(), which is a PyQt-only thing. Try this:

                  # in `__init__()`
                  self.tab1 = uic.loadUi('SPTMain.ui')
                  
                  # in `handleAddTab()`
                  self.tabWidget.addTab(self.tab1, 'Tab One')
                  
                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    aadil50
                    wrote on last edited by
                    #9

                    It crash the window app...

                    JonBJ 1 Reply Last reply
                    0
                    • A aadil50

                      It crash the window app...

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #10

                      @aadil50
                      The following works for me. I had to hack together a couple of things (including changing from main window to a generic widget) since I do not have (nor want!) whatever you have.

                      import sys
                      from PyQt5 import QtCore, QtGui, QtWidgets,uic
                      
                      class TestGUI(QtWidgets.QWidget):
                          def __init__(self):
                              super(TestGUI, self).__init__()
                              layout = QtWidgets.QVBoxLayout(self)
                              self.NewTab = QtWidgets.QPushButton("Create Tab", self)
                              layout.addWidget(self.NewTab)
                              self.tabWidget = QtWidgets.QTabWidget(self)
                              layout.addWidget(self.tabWidget)
                              self.tab1 =  uic.loadUi('/path/to/somefile.ui')
                              self.NewTab.clicked.connect(self.handleAddTab)
                      
                          def handleAddTab(self):
                              self.tabWidget.addTab(self.tab1, 'Tab One')
                      
                      if __name__ == '__main__':
                          app = QtWidgets.QApplication(sys.argv)
                          window = TestGUI()
                          window.show()
                          sys.exit(app.exec_()) 
                      
                      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