PyQt - QToolBox Widget



The QToolBox is a container widget in PyQt that is used to organize mutiple pages of content just like multiple tabs in a browser. Each page contains a collection of tools or information related to a specific category or topic. QToolBox is often used in applications where the user needs to navigate through diffrent sets of tools or options.

Inheritance

QToolBox inherits its properties from QFrame class which in turn inherits from QWidget class.

Components of QToolBox

There are primarily to main components of QtoolBox −

  • Tabs − The tabs are the labelled button present at the top of the widget. Clicking on the tab changes the visible area to specific contetn of the page. This is similar to tabs in browsers.
  • Pages − These are individual widgets or layouts that contain application specific functionalities related to the tab level.

Features of QToolBox Widget

  • Page-Based Organization − The content of the QToolBox is organized into pages. Each page has a title and respective set of widget and controls.
  • Expandable/Collapsible Pages − Pages in QToolBox can be expanded and collapsed to reduce screen-space and provide a clean interface for the user.
  • Customizable Appearance − Developers can coustomize the appearence of the QToolBox widget by changing thier style,size and layout of the pages.
  • Signal-Slot Mechanism − QToolBox provides signals that can be connected to slots which helps in responding user intercations and change the widget's state.

Methods in QTollBox

Sr.No. Modules & Description
1

addItem()

Adds the widget in a new tab at the bottom of the toolbox.

2

count()

Counts the number of items contained in the toolbox.

3

currentIndex()

Returns the index of the current item.

4

insertItem()

Inserts the widget at position index , or at the bottom of the toolbox if index is out of range

5

itemToolTip()

Returns the tooltip of the item at a specific index. Returns empty string if the index is out of range.

6

itemText()

Returns the text of the item at a specific index or an empty string if the index is out of range..

7

itemIcon()

Returns the icon present in at the specif item index.

8

isItemEnabled()

Returns true if the item at specific index is enabled else returns false

9

removeItem()

Removes the itme at the specific index from the tooltip.

10

setItemEnabled()

If enabled is true then the item at specific index is enabled else the item at specific index is disabled.

11

setItemIcon()

Sets the icon of the item at specific index.

12

setItemText()

Sets the text of the item at specific index or position.

Example: Document Viewer with Multiple Tabs

In the below example, we will create a simple document viewer application using Qtoolbox to display multiple document in seperated tabs. We can switch between the documents by selecting different tabs.

import sys
from PyQt6.QtWidgets import QApplication, QToolBox, QTextEdit

class DocumentViewer(QToolBox):
   def __init__(self):
      super().__init__()
      self.setWindowTitle("Document Viewer")

      self.document1 = QTextEdit()
      self.document2 = QTextEdit()

      self.addItem(self.document1, "Document 1")
      self.addItem(self.document2, "Document 2")

def main():
   app = QApplication(sys.argv)
   viewer = DocumentViewer()
   viewer.show()
   sys.exit(app.exec())

if __name__ == "__main__":
   main()

Output

The above code produces the following output −

pyqt qtoolbox example
Advertisements