PySide6 Basic Signal and Slot 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:
-
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 fromclicked
,QPushButton
sendspressed
,released
andtoggled
signals.clicked
combinespressed
andreleased
. -
Add a method to
Window
that will be called when the button is clicked. This is our slot and it is marked with theSlot()
decorator. You don’t have to use theSlot()
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 theon_button_clicked
signature you’ll see that it accepts a parameter namedchecked
. The signature ofon_button_clicked
matches the signature ofQPushButton.clicked
. In this examplechecked
is alwaysFalse
because itscheckable
property is set toFalse
by default and we didn’t change it. The signature could have beenon_button_clicked(self)
as well in which casechecked
would have been ignored.
Our slot simply prints a message in the terminal but any code you put there will be executed onbutton
click. -
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 afterself.on_button_clicked
becausebutton.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.