Events, Signals and Slots

Learn about events, signals and slots with PyQt6.


Events, signals, and slots are crucial notions for PyQt GUI application development. GUIs are event driven, which means they react to user-created events, such as those made by the keyboard or mouse, and system generated events, such as those brought on by timers or Bluetooth connections. The application must pay attention to these events, also known as event handling, regardless of how they are produced. For instance, the program starts to listen to events when exec() is called and continues to do so until it is closed. Signals and slots are used in PyQt to handle events.

Events, signals, and slots in action

Events also referred to as signals, occur when a widget's state changes, such as whenever a button is pressed or a checkbox is toggled. The procedures carried out in response to the signal are called slots. Slots are built-in PyQt or Python routines tied to an event and run whenever a signal is received. We must connect() to some callable method—in this case buttonClicked()—which is the slot for that signal. Simply put, widgets emit signals which we gather and use in conjunction with slots to cause our program to do some action. Numerous devices already have predefined signals and slots, so all you need to do is contact them to obtain the desired behavior for your application.

Creating custom signals in PyQt

Let's now examine how to modify a widget's style sheet using a custom signal created using pyqtSignal.

First, we start by importing the modules that will be needed from both QtWidgets and QtCore. The QtCore module is imported, along with the pyqtSignal factory and QObject classes. The mechanics for signals and slots are provided by the QtCore module and QObject classes.

Get hands-on with 1200+ tech skills courses.