PyQt - QTab Widget


If a form has too many fields to be displayed simultaneously, they can be arranged in different pages placed under each tab of a Tabbed Widget. The QTabWidget provides a tab bar and a page area. The page under the first tab is displayed and others are hidden. The user can view any page by clicking on the desired tab.

Following are some of the frequently used methods of QTabWidget class −

Given below are the most commonly used methods of QTabWidget.

Sr.No. Methods & Description
1

addTab()

Adds a tab associated with a widget page

2

insertTab()

Inserts a tab with the page at the desired position

3

removeTab()

Removes tab at given index

4

setCurrentIndex()

Sets the index of the currently visible page as current

5

setCurrentWidget()

Makes the visible page as current

6

setTabBar()

Sets the tab bar of the widget

7

setTabPosition()

Position of the tabs are controlled by the values

QTabWidget.North above the pages

QTabWidget.South below the pages

QTabWidget.West to the left of the pages

QTabWidget.East to the right of the pages

8

setTabText()

Defines the label associated with the tab index

The following signals are associated with QTabWidget object −

Sr.No. Methods & Description
1

currentChanged()

Whenever the current page index changes

2

tabClosedRequested()

When the close button on the tab is clicked

Example

In the following example, the contents of a form are grouped in three categories. Each group of widgets is displayed under a different tab.

Top level window itself is a QTabWidget. Three tabs are added into it.

self.addTab(self.tab1,"Tab 1")
self.addTab(self.tab2,"Tab 2")
self.addTab(self.tab3,"Tab 3")

Each tab displays a sub form designed using a layout. Tab text is altered by the statement.

self.setTabText(0,"Contact Details")
self.setTabText(1,"Personal Details")
self.setTabText(2,"Education Details")

The complete code is as follows −

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class tabdemo(QTabWidget):
   def __init__(self, parent = None):
      super(tabdemo, self).__init__(parent)
      self.tab1 = QWidget()
      self.tab2 = QWidget()
      self.tab3 = QWidget()
		
      self.addTab(self.tab1,"Tab 1")
      self.addTab(self.tab2,"Tab 2")
      self.addTab(self.tab3,"Tab 3")
      self.tab1UI()
      self.tab2UI()
      self.tab3UI()
      self.setWindowTitle("tab demo")
		
   def tab1UI(self):
      layout = QFormLayout()
      layout.addRow("Name",QLineEdit())
      layout.addRow("Address",QLineEdit())
      self.setTabText(0,"Contact Details")
      self.tab1.setLayout(layout)
		
   def tab2UI(self):
      layout = QFormLayout()
      sex = QHBoxLayout()
      sex.addWidget(QRadioButton("Male"))
      sex.addWidget(QRadioButton("Female"))
      layout.addRow(QLabel("Sex"),sex)
      layout.addRow("Date of Birth",QLineEdit())
      self.setTabText(1,"Personal Details")
      self.tab2.setLayout(layout)
		
   def tab3UI(self):
      layout = QHBoxLayout()
      layout.addWidget(QLabel("subjects")) 
      layout.addWidget(QCheckBox("Physics"))
      layout.addWidget(QCheckBox("Maths"))
      self.setTabText(2,"Education Details")
      self.tab3.setLayout(layout)
		
def main():
   app = QApplication(sys.argv)
   ex = tabdemo()
   ex.show()
   sys.exit(app.exec_())
	
if __name__ == '__main__':
   main()

The above code produces the following output −

QTabWidget Output