PySide6 Basic Signal and Slot Example

Basic example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Signals are event notifications.
# Slots are Python methods/functions.
# A QPushButton is a button that you can push.

import sys
from PySide6.QtCore import Slot
from PySide6.QtWidgets import (QApplication, 
    QWidget, QPushButton, QVBoxLayout)


class Window(QWidget):
    
    def __init__(self):

        super().__init__()
        
        layout = QVBoxLayout()
        self.setLayout(layout)
        
        # 1 - Create the signal sender, ie. the QPushButton.
        #     When you click it the button emits a signal.
        
        button = QPushButton('Click me!')
        
        # 3 - Connect the signal and the slot.
        #     Notice there's no parentheses after self.on_button_clicked
        #     This means you pass a Python function object
        #     to the connect() method
        
        button.clicked.connect(self.on_button_clicked)
        
        layout.addWidget(button)
        
    # 2 - Create the slot. It's a simple method
    #     that belongs to our Window class.
    #     What ever code you put here is
    #     executed when the button is clicked.
    
    @Slot()
    def on_button_clicked(self, checked):
        print('Button clicked,', 'checked:', checked)
        

if __name__ == '__main__':
    
    app = QApplication(sys.argv)

    main_window = Window()
    main_window.show()
    
    sys.exit(app.exec())

The signals and slots mechanism is a fundamental concept in Qt. A signal is emitted when an event (eg. mouse click) occurs and a slot is a Python method or function that is executed when a signal is emitted. In order for a slot to actually be called you need to connect the two. So, starting with the template PySide6 script:

  1. Create a QPushButton instance and add it to the layout. QPushButton is a common widget that sends a signal when you click on it. Aside from clicked, QPushButton sends pressed, released and toggled signals. clicked combines pressed and released.

  2. Add a method to Window that will be called when the button is clicked. This is our slot and it is marked with the Slot() decorator. You don’t have to use the Slot() decorator and the code will still work, but the documentation that you do as not doing so causes run-time overhead.
    If you look at the on_button_clicked signature you’ll see that it accepts a parameter named checked. The signature of on_button_clicked matches the signature of QPushButton.clicked. In this example checked is always False because its checkable property is set to False by default and we didn’t change it. The signature could have been on_button_clicked(self) as well in which case checked would have been ignored.
    Our slot simply prints a message in the terminal but any code you put there will be executed on button click.

  3. Connect the signal and the slot. The Python syntax to make the connection is object.signal_name.connect(slot_name). Note there are no parentheses after self.on_button_clicked because button.clicked.connect() accepts a function object.

That’s about it for this example but signals and slots is a large and an important topic and there’s much more to it than this simple script.