PyQt - Major Classes



Modules in PyQt

PyQt API is a large collection of classes and methods. These classes are defined in more than 20 modules. Following are some of the frequently used modules −

Sr.No. Modules & Description
1

QtCore

Core non-GUI classes used by other modules

2

QtGui

Graphical user interface components

3

QtMultimedia

Classes for low-level multimedia programming

4

QtNetwork

Classes for network programming

5

QtOpenGL

OpenGL support classes

6

QtScript

Classes for evaluating Qt Scripts

7

QtSql

Classes for database integration using SQL

8

QtSvg

Classes for displaying the contents of SVG files

9

QtWebKit

Classes for rendering and editing HTML

10

QtXml

Classes for handling XML

11

QtAssistant

Support for online help

12

QtDesigner

Classes for extending Qt Designer

Classes in PyQt

PyQt API contains more than 400 classes. The QObject class is at the top of class hierarchy. It is the base class of all Qt objects. Additionally, QPaintDevice class is the base class for all objects that can be painted.

QApplication class manages the main settings and control flow of a GUI application. It contains main event loop inside which events generated by window elements and other sources are processed and dispatched. It also handles system-wide and application-wide settings.

QWidget class, derived from QObject and QPaintDevice classes is the base class for all user interface objects. QDialog and QFrame classes are also derived from QWidget class. They have their own sub-class system.

Following diagrams depict some important classes in their hierarchy.

Hierarchy QWidget QDialog QIODevice QPaintDevice

Here is a select list of frequently used widgets −

Given below are the commonly used Widgets.

Sr.No. Widgets & Description
1

QLabel

Used to display text or image

2

QLineEdit

Allows the user to enter one line of text

3

QTextEdit

Allows the user to enter multi-line text

4

QPushButton

A command button to invoke action

5

QRadioButton

Enables to choose one from multiple options

6

QCheckBox

Enables choice of more than one options

7

QSpinBox

Enables to increase/decrease an integer value

8

QScrollBar

Enables to access contents of a widget beyond display aperture

9

QSlider

Enables to change the bound value linearly.

10

QComboBox

Provides a dropdown list of items to select from

11

QMenuBar

Horizontal bar holding QMenu objects

12

QStatusBar

Usually at bottom of QMainWindow, provides status information.

13

QToolBar

Usually at top of QMainWindow or floating. Contains action buttons

14

QListView

Provides a selectable list of items in ListMode or IconMode

15

QPixmap

Off-screen image representation for display on QLabel or QPushButton object

16

QDialog

Modal or modeless window which can return information to parent window

A typical GUI based applications top level window is created by QMainWindow widget object. Some widgets as listed above take their appointed place in this main window, while others are placed in the central widget area using various layout managers.

The following diagram shows the QMainWindow framework −

QMainWindow

Examples of some major classes in PyQt

1. QMainWindow: The Foundation of GUI Applications

QMainWindow represents the main application window, providing a centralized hub for organizing widgets, menus, toolbars, and more. By inheriting from QMainWindow, developers can quickly create robust application skeletons with minimal boilerplate code.

from PyQt6.QtWidgets import QMainWindow, QApplication
import sys
class MainWindow(QMainWindow):
   def __init__(self):
      super().__init__()
      self.initUI()

   def initUI(self):
      # Initialize and configure UI elements
      self.setWindowTitle("PyQt Major Classes Demo")

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show()
   sys.exit(app.exec())
Output
PyQt Main Window

2. QWidget: The Building Block of User Interfaces

In PyQt, QWidget is used as the fundamental building block for constructing user interfaces. From buttons to text inputs, every visible element on the screen is ultimately derived from the QWidget class.

from PyQt6.QtWidgets import QMainWindow, QApplication, QWidget
import sys
class CustomWidget(QWidget):
   def __init__(self):
      super().__init__()
      self.initUI()

   def initUI(self):
      # Initialize and configure custom UI elements
      self.setWindowTitle("PyQt Qwidget class")

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = CustomWidget()
   window.show()
   sys.exit(app.exec())
Output
PyQt Qwidget

3. QLabel, QPushButton, and Beyond: Specialized Widgets

Beyond the core classes like QMainWindow and QWidget, PyQt offers many specialized widgets catering to diverse use cases. QLabel facilitates the display of text and images, QPushButton enables interactive buttons, while QComboBox and QCheckBox streamline user input and selection.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget

class MainWindow(QMainWindow):
   def __init__(self):
      super().__init__()
      self.initUI()

   def initUI(self):
      self.setWindowTitle("PyQt Major Classes Demo")

      # Create QLabel
      label = QLabel("Hello, PyQt!")

      # Create QPushButton
      button = QPushButton("Click Me")
      button.clicked.connect(self.onButtonClick)

      # Create a vertical layout
      layout = QVBoxLayout()
      layout.addWidget(label)
      layout.addWidget(button)

      # Create a central widget and set the layout
      central_widget = QWidget()
      central_widget.setLayout(layout)
      self.setCentralWidget(central_widget)

   def onButtonClick(self):
      print("Button clicked!")

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show()
   sys.exit(app.exec())
Output
PyQt Other Classes
Advertisements