Python PySide6
About Lesson

In this PySide6 lesson we want to learn about PySide6 Signals and Slots, In GUI programming, communication between objects are important for building complex and interactive applications. Signals and slots in PySide6 are a mechanism for implementing such communication. Signals are emitted by objects when an event occurs, and slots are functions that can be executed in response to a signal.

 

 

Signals and Slots in Pyside6

Signals and slots mechanism in Pyside6 works by connecting a signal emitted by an object to a slot function. The connection is established using the connect() method of the signal object. connect() method takes slot function as arguments.

 

 

How to Create PySide6 Signals and Slots

 

For example, let’s see a simple GUI application, for example when a button is clicked, we want to display a message. We can define a slot function for this purpose as follow:

 

 

For connecting this slot function to the clicked signal of a button, we can use the following code:

In the above example button is an instance of the QPushButton class, and clicked is a signal emitted by the button when it is clicked. The connect() method connects the clicked signal to the display_message() slot function, so that when the button is clicked, display_message() function is executed.

 

 

How to Pass Arguments to PySide6 Slots

Slots can also take arguments when they are executed. For example, consider a GUI application with a slider that controls the font size of a text label. We can define a slot function that takes the value of the slider as an argument:

 

 

For connecting this slot function to the valueChanged signal of a slider, we can use the following code:

In the above code slider is an instance of the QSlider class, and valueChanged is a signal emitted by the slider when its value changes. connect() method connects the valueChanged signal to the set_font_size() slot function, passing the value of the slider as an argument.

 

 

Disconnecting Signals and Slots

It is also possible to disconnect a signal from a slot using disconnect() method. For example, to disconnect the clicked signal from the display_message() slot function, we can use the following code:

Here, the disconnect() method disconnects the clicked signal from the display_message() slot function.

 

 

This is the complete code for a simple GUI application that shows the use of signals and slots in Pyside6:

This code defines MyWindow class that inherits from QMainWindow and defines a simple GUI with a button, a slider and a label. display_message() slot function is connected to the button’s clicked signal, and set_font_size() slot function is connected to the slider’s valueChanged signal.

When we click button, the display_message() function is executed, and it simply prints a message to the console. When the slider’s value changes, the set_font_size() function is executed, and it updates the font size of the label according to the value of the slider.

 

 

 

Run the code and this will be the result

PySide6 Signals and Slots
PySide6 Signals and Slots
×