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
Advertisements