PyQt - QTabBar



A Tab bar is a simple navigation among different areas such as clock time, calculator, stopwatch, etc. In PyQt, QTab class provides the structures in a QTabBar. A QTabBar is like a menu that helps you navigate through your app, particularly when you have different sections or pages to show. Think of it as a row of buttons at the top or bottom of the app window, with each button labeled with a title. Each button represents a different part or feature of the app. For example, in a web browser, we have tabs that represents different websites which we are viewing. In a music player, we have tabs for playlists, artists, or albums. When we click on a button, it tells the app to show us the corresponding part of the app. It is like turning pages in a book, but on a digital screen.

Uses of QTabBar in PyQt

To create a QTabBar, we will need to specify its parent (the widget where it belongs to). By default, it displays tabs horizontally, but we can customize it to be vertical if needed.

We can add tabs dynamically using the addTab() function. Each tab can have a label (text) and an optional icon. For example, if we are building a music player, we might have tabs labeled "Playlists", "Artists", and "Albums".

When a user clicks on a tab, the corresponding content (view or section) associated with that tab becomes visible. This is a best way to organize and switch between different parts of your application.

QTabBar VS QTabWidget

In PyQt, QTabBar and QTabWidget are closely related components which can be used for creating a tabbed interface.

Let's explain how both these are interact to each other.

  • QTabBar − This is a component that provides a horizontal row of tabs. The tab bar is responsible for management and display of tabs. We can customize the tabs such as setting the tab's name, icons and tooltips.
  • QTabWidget − This is a higher level components that combines QTabBar with a stack of widgets. Each tabs of QTabBars correspondance to the class QTabBars. When user select any tabs , it display only associate tabs and hidden all other. Thus, this makes a convinient way to switch between different tabs or views.

Note that QTabBar simplifies navigation and keeps your app organized. Whether we are building a complex software suite or a simple utility, tabs can help users find what they need.

Example 1

In this example, we create a three widgets to display the tab bars in PyQt.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QTabBar
class EventHandling(QMainWindow):
   def __init__(self):
      super().__init__()
      self.initUI()
   def initUI(self):
      self.setWindowTitle("QTabBar")
      self.setGeometry(100, 100, 600, 400)

      tab_bar = QTabBar(self)
      tab_bar.addTab("Tab 1")
      tab_bar.addTab("Tab 2")
      tab_bar.addTab("Tab 3")

      tab_bar.currentChanged.connect(self.tab_changed)
      self.setCentralWidget(tab_bar)

   def tab_changed(self, index):
      print(f"Tab changed to index {index}")

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = EventHandling()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output−

qtabbars_ex_one

Example 2

In this example, we are creating two widgets that represent the opening of any page using

import sys
from PyQt6.QtWidgets import QMainWindow, QApplication, QWidget, QTabWidget, QVBoxLayout
class App(QMainWindow):

   def __init__(self):
      super().__init__()
      self.table_widget = MyTableWidget(self)
      self.setCentralWidget(self.table_widget)
      self.show()
class MyTableWidget(QWidget):
   def __init__(self, parent):
      super(QWidget, self).__init__(parent)
      self.layout = QVBoxLayout(self)

      self.tabs = QTabWidget()
      self.tabs.tabBar().setExpanding(True)
      self.tab1 = QWidget()
      self.tab2 = QWidget()
      self.tabs.resize(300, 200)

      self.tabs.addTab(self.tab1, "Python")
      self.tabs.addTab(self.tab2, "JAVA")

      # Create first tab
      self.tab1.layout = QVBoxLayout(self)

      self.layout.addWidget(self.tabs)
      self.setLayout(self.layout)

   def resizeEvent(self, event):
      super().resizeEvent(event)
      self._set_tabs_width()

   def showEvent(self, event):
      super().showEvent(event)
      self._set_tabs_width()

   def _set_tabs_width(self):
      tabs_count = self.tabs.count()
      tabs_width = self.tabs.width()
      tab_width = tabs_width / tabs_count
      css = "QTabBar::tab {width: %spx;}" % tab_width
      self.tabs.setStyleSheet(css)

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = App()
   sys.exit(app.exec())

Output

The above code produces the following output−

qtabbars_ex_two
Advertisements