A PySide6 Signal and Slot Example using Lambdas

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
53
54
# If the signature of a signal matches 
# the signature of the receiving slot
# you can use the signal's arguments in the slot.
# If a slot has a shorter signature 
# than the signal it is connected with 
# the extra arguments are ignored

# Demonstrate using a Python lambda function
# as a slot. Lambdas are anonymous functions,
# ie. they have no name.


import sys

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


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

        super().__init__()
        
        layout = QVBoxLayout()
        self.setLayout(layout)
        
        # 1 - Create the widget, same as in the basic example
        
        button = QPushButton('Click me!')
        button.setCheckable(True)
        
        # 2 - In this case the slots are Python lambdas
        #     placed in the button.clicked.connect() call.
        #     We have two slots connected to the same signal.

        button.clicked.connect(lambda: print('Clicked...'))
        button.clicked.connect(lambda checked: print(checked, 'Something else'))
        
        layout.addWidget(button)
        

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

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

The pattern is the same as in the basic signals-slots example:

  1. Create the widget. It’s the same QPushButton as in the first script only now we set its checkable() property to True so it can double as a toggle button. Without this its checked property doesn’t toggle on mouse click.

  2. Connect the QPushButton.clicked() signals with slots. There are three things to note here:

    • You can connect a Python lambda function (ie. a function without a name) instead of an ordinary method/function to a signal,

    • There are two slots (in this case lambdas) connected to the same signal and each will be called when the signal is emitted. You can also connect multiple signals with a single slot.

    • You can use a lambda to pass parameters to a slot. In the script, checked that matches the signal signature, but 'Something else' is additional data passed to the actual function that will handle the signal.

(Can you add the @Slot() annotation to a lambda?)