This article is part of in the series
Last Updated: Wednesday 29th December 2021

In the last installment, we learned how to create and set up interactive widgets, as well as how to arrange them into simple and complex layouts using two different methods. Today, we're going to discuss the Python/Qt way of allowing your application to respond to user-triggered events: signals and slots.

When a user takes an action — clicking on a button, selecting a value in a combo box, typing in a text box — the widget in question emits a signal. This signal does nothing, by itself; it must be connected to a slot, which is an object that acts as a recipient for a signal and, given one, acts on it.

Connecting Built-In PySide/PyQt Signals

Qt widgets have a number of signals built in. For example, when a QPushButton is clicked, it emits its clicked signal. The clicked signal can be connected to a function that acts as a slot (excerpt only; more code is needed to make it run):

Note the use of the @Slot() decorator above the definition of clicked_slot; though not strictly necessary, it provides the C++ Qt library hints on how clicked_slot should be called. (For more information on decorators, see the Python Decorators Overview article.) We'll see more information on the @Slot macro later. For now, know that when the button is clicked, it will emit the clicked signal, which will call the function to which it is connected; having a juvenile sense of humor, it will print, 'Ouch!'.

For a less puerile (and actually executable) example, let's look at how a QPushButton emits its three relevant signals, pressed, released, and clicked.

When you run the application and click the button, it will print:

The pressed signal is emitted when the button is pressed down, the released signal when it is released, and finally, when both those actions are complete, the clicked signal is fired.

Completing Our Example Application

Now, it's easy to complete our example application from the previous installment. We'll add a slot method to the LayoutExample class that will show the constructed greeting:

Note that we use the recipient QLineEdit's text() method to retrieve the text the user entered there, and the salutation QComboBox's currentIndex() method to get the index of the salutation the user selected. We also used the Slot() decorator to indicate that show_greeting will be used as a slot.

Then, we can simply connect the build button's clicked signal to that method:

Our final example, in total, then looks like this:

Run it, and you will get the same window as before, except now it actually generates our greeting when the Build button is pressed. (Note that the same methods could be added to our absolute-positioning example from last time with the same effect.)

Now that we have an idea how to connect built-in signals to slots that we create, we are ready for our next installment, in which we will learn how to create our own signals and connect them to slots.

About The Author