PyQt6 Signals and Slots

In this PyQt6 Tutorial we will discuss the Signals and Slots system. Signals, as the name implies are used as indicators for when events occur. Whenever an event, such as a Button Click happens, the appropriate signal is generated to inform us that the event has occurred.

A signal by itself, does not have any practical application however, as it merely serves as an Indicator. We need a system which actually receives the signal, and executes the appropriate code to handle that event. This is where Slots come in.

Throughout this tutorial we will explore how we can use Signals and Slots together in a variety of applications.


Understanding Signals

Let’s take a look at Signals in PyQt6 in more detail. Every widget has various signals that it can emit, based on various events that could occur when the user interacts with it. The most common example of Signals, is with the Button widget when we click it.

Let’s take a look at a bunch of different signals belonging to various widgets to further our understanding of when signals usually occur.

QPushButton Widget:

  1. clicked() -> Occurs when the Button is clicked and released.
  2. pressed() -> Occurs when the Button is clicked and held down
  3. released() -> Occurs when the Button is released (after being clicked)

QSlider Widget:

  1. valueChanged() -> Occurs when the value on the Slider has been changed (either through the user or through code)
  2. sliderMoved() -> Occurs when the User moves the Slider
  3. sliderPressed() -> Occurs when the User “starts” to move the Slider

QLineEdit Widget:

  1. textChanged() -> Activates when the text in the QLineEdit widget is changed in any manner.
  2. textEdited() -> Almost the same as textChanged(), however this signal is not emitted when the text is changed programmatically.
  3. editingFinished() -> This signal is emitted when the Return or Enter key is pressed or the line edit loses focus (when it’s no longer highlighted)
  4. returnPressed() -> Emitted when either the return key or enter key is pressed by the user.

Connecting Signals to Slots

Now that we understand what signals are, let’s try connecting a few of them to a “slot”. The term “slot” in PyQt is actually very vague and simply refers to a function that is connected to a signal. In order to connect a Signal to a Slot, we will use the connect() function.

Let us make this clear with a simple example using a Button, and a function that gets triggered when it is executed.

import sys
from PyQt6.QtWidgets import QWidget, QPushButton, QApplication

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(250, 200)
        self.setWindowTitle("CodersLegacy")

        Button = QPushButton("A Button", self)
        Button.clicked.connect(self.handleClick)
        Button.move(90, 80)

        self.show()

    def handleClick(self):
        print("The Button was Clicked")

app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec())

After clicking the button three times, we see the following output.

The Button was Clicked
The Button was Clicked
The Button was Clicked

In the above example, the signal is “clicked”, and the slot is “handleClick”.

Let’s try another example with the QLineEdit Widget.

import sys
from PyQt6.QtWidgets import QWidget, QLineEdit, QApplication

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(250, 200)
        self.setWindowTitle("CodersLegacy")

        input = QLineEdit(self)
        input.editingFinished.connect(self.handleInput)
        input.move(60, 60)

        input2 = QLineEdit(self)
        input2.move(60, 100)

        self.show()

    def handleInput(self):
        print("Input Finished")
        print("Submitting Input....")

app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec())

The below output will be printed whenever you press enter on the first input box, or after you enter some text in the first input box, then click on the second input box. (This is because the focus moves off the first input box)

Input Finished
Submitting Input....

This marks the end of the PyQt6 Signals and Slots Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments