
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - Connecting, Disconnecting, and Emitting Signals
Connecting Signals to Slots
In PyQt, signals are connected to slots using the connect() function of a bound signal. The connect() method returns a Connection object when the signal is successfully connected to the slots. It can also disconnected later. If the connection establishment fails, an exception will be raised.
Syntax and parameters of connecting signal
The syntax and the paremeters passed for connecting a signal are as follows −
connect(slot[, type=PyQt5.QtCore.Qt.AutoConnection[, no_receiver_check=False]]) PyQt5.QtCore.QMetaObject.Connection
The parameters passed serve different purpose as follows −
- slot − It specifies the slot to which the signal is to be connected. It can either be a Python callable or another bound signal.
- type − The type of the connection to make. By default, it's set to AutoConnection, meaning PyQt will determine the most appropriate type of connection.
- no_receiver_check − It is an optional parameter to suppress the check that the underlying C++ receiver instance still exists and deliver the signal anyway.
Example of Connecting Signal
In this example, a class Foo is defined with a signal trigger. The trigger signal is then connected to a slot handle_trigger using a method connect_and_emit_trigger().
from PyQt6.QtCore import QObject, pyqtSignal class Foo(QObject): trigger = pyqtSignal() def connect_and_emit_trigger(self): self.trigger.connect(self.handle_trigger) self.trigger.emit() def handle_trigger(self): print("Trigger signal received") foo = Foo() foo.connect_and_emit_trigger()
Output
Trigger signal received
Disconnecting Signals from Slots
Signals can be disconnected from slots using the disconnect() method of a bound signal. If the slot is not connected to the signal or if the signal has no connections at all then it will raise an exception.
Syntax and parameters of disconnecting signal
disconnect([slot])
Here, slot is an optional parameter whihc specifies the slot to disconnect from. If not passed, all the slots connected to the signal are disconnected.
Example of Disconnecting Signal
In this example, we define a class Bar with a signal trigger and a method connect_and_disconnect_trigger() to connect the signal to a slot handle_trigger(). When connect_and_disconnect_trigger() is called, it connects trigger signal to handle_trigger() slot, emits the signal, prints "Trigger signal received", and then disconnects the slot.
from PyQt6.QtCore import QObject, pyqtSignal class Bar(QObject): trigger = pyqtSignal() def connect_and_disconnect_trigger(self): self.trigger.connect(self.handle_trigger) self.trigger.emit() self.trigger.disconnect(self.handle_trigger) def handle_trigger(self): print("Trigger signal received") bar = Bar() bar.connect_and_disconnect_trigger()
Output
Trigger signal received
Emitting Signals
Signals are emitted using the emit() method of a bound signal.
Syntax and parameters of Emitting signals
emit(*args)
Here, args is an optional sequence of arguments to pass to any connected slots.
Example of Emitting Signal
In this example a class Baz is defined which contains a signal trigger. We then connect the trigger signal to a slot handle_trigger() and emitthe signal with value 42. When the connected slot receives this value it prints Trigger signal received with value: 42 as output.
from PyQt5.QtCore import QObject, pyqtSignal class Baz(QObject): trigger = pyqtSignal(int) def emit_trigger(self): self.trigger.emit(42) def handle_trigger(self, value): print("Trigger signal received with value:", value) baz = Baz() baz.trigger.connect(baz.handle_trigger) baz.emit_trigger()
Output
Trigger signal received with value: 42