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
Advertisements