
- 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
The pyqtSlot() Decorator
In PyQt, pyqtSlot() decorator allows python methods to explicitly marked as Qt slots which can define their C++ signatures. In this chapter we will understand the working of pyqtSlot() decorator, its parameter and its usage.
Introduction to pyqtSlot() Decorator
In PyQt it enables any Python callable to function as a slot when connecting it to signals. In some cases there is a need to explicitly define a Python method as Qt slot. We can do this using pyqtSlot() function decorator. It can explicitly mark Python methods as Qt slots and provides a means to specify their C++ signatures.
Syntax and parameters
The syntax for the pyqtSlot() decorator is as follows −
PyQt5.QtCore.pyqtSlot(types[, name[, result[, revision=0]]])
The difeerent parameters passed to pyqtSlot() function serves different purpose as follows −
- types − It defines the types that constitute the C++ signature of the slot. Each type can be a Python type object or a string representing a C++ type.
- name:(optional) − It specifies the name of the slot visible to C++. If omitted, the name of the decorated Python method is used. This parameter is provided as a keyword argument.
- revision:(optional) − Denotes the revision of the slot exported to QML. This parameter is specified as a keyword argument.
- result:(Optional) − Indicates the type of the result. It can be a Python type object or a string specifying a C++ type. This parameter is provided as a keyword argument.
Advantages of pyqtSlot() Decorator
Connecting a signal to a decorator Python method using pyqtSlot() decorator has few advantages as follows −
- It enhances the memory efficiency by reducing the memmory usage.
- It also improves the performance slightly and makes the signal connection faster.
Example 1: Simple Slot Definition using pyqtSlot() Decorator
In this example, we define a class Foo inheriting from QObject. Within Foo, we declare a method foo() and decorate it with @pyqtSlot(), indicating that it's a Qt slot. This slot takes no arguments. When an instance of Foo is created and foo() is called, it prints a message indicating that the slot has been called.
from PyQt5.QtCore import QObject, pyqtSlot class Foo(QObject): @pyqtSlot() def foo(self): print("Simple slot foo() called") # Create an instance of Foo foo_instance = Foo() # Call the slot foo_instance.foo()
Output
Simple slot foo() called
Example 2: slot with multiple arguments using pyqtslot() decorator
In this example we define a class Foo inheriting from QObject. The method foo() is decorated with @pyqtSlot(int, str), specifying that it's a slot that takes an integer and a string as arguments. When this slot is called with appropriate arguments, it prints a message along with the values of the provided arguments.
from PyQt5.QtCore import QObject, pyqtSlot class Foo(QObject): @pyqtSlot(int, str) def foo(self, arg1, arg2): print("Slot foo() called with arguments:", arg1, arg2) # Create an instance of Foo foo_instance = Foo() # Call the slot foo_instance.foo(10, "Hello")
Output
Slot foo() called with arguments: 10 Hello
Example 3: slot with specified name and result
In this example, within the classFoo, we define a method foo() as a slot and decorate it with @pyqtSlot(int, name='bar'). This defines a slot named bar() that takes an integer argument. When called with an integer argument, it prints a message indicating that the bar() slot has been called along with the provided argument.
from PyQt5.QtCore import QObject, pyqtSlot class Foo(QObject): @pyqtSlot(int, name='bar') def foo(self, arg1): print("Slot bar() called with argument:", arg1) # Create an instance of Foo foo_instance = Foo() # Call the slot foo_instance.foo(20)
Output
Slot bar() called with argument: 20