Pyqt Signal Slot Emit 7,7/10 4202 votes

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). Signals and Slots in PySide. From Qt Wiki (Redirected from Signals and slots in PySide) Redirect page. Jump to: navigation, search. The receiving slot can use this data to perform different actions in response to the same signal. However, there is a limitation: the signal can only emit the data it was designed to. So for example, a QAction has a.triggered that fires when that particular action has been activated. Each PyQt widget, which is derived from QObject class, is designed to emit ‘ signal ’ in response to one or more events. The signal on its own does not perform any action. Instead, it is ‘connected’ to a ‘ slot ’. The slot can be any callable Python function. QT signal & slot VS Python signal & slot All the predefined signals & slots provided by pyqt are implemented by QT's c code. Whenever you want to have a customized signal & slot in Python, it is a python signal & slot. Hence there are four cases to emits a signal to a slot: from a QT signal to a QT slot; from a QT signal to a Python slot.

I am reading through some documentation on PyQt5 to come up with a simple signal-slot mechanism. I have come to a halt due to a design consideration.

Consider the following code:

To track the changes made to the slider, I simply print and log the changes made. What I do not like about the code is that I am required to call the sld.valueChanged slot thrice to send the same information to 3 different slots.

Is it possible to create my own pyqtSignal that sends an integer to a single slot function. And in turn have the slot function emit the changes that need to be made?

  • Maybe I don't fully understand the purpose of emit() because there are no good examples of it's purpose in the PyQt Signal-Slot docs. All we're given is an example of how to implement an emit with no parameters.

What I would like to do is create a function that handles the emit function. Consider the following:

There are obviously some serious design flaws here. I cannot wrap my head around the order of function calls. And I am not implementing pyqtSignal correctly. I do however believe that correctly stating the following 3 points will help me produce a proper app:

  1. For a predefined signal: send the signal to the slot function. Slot can be reimplemented to use the signal values.
  2. Produce pyqtSignal object with some parameters. It is not yet clear what the purpose of these parameters are and how they differ from 'emit' parameters.
  3. emit can be reimplemented to send specific signal values to the slot function. It is also not yet clear why I would need to send different values from previously existing signal methods.

Feel free to completely alter the code for what I am trying to do because I have not yet figured out if its in the realm of good style.

Signals are a neat feature of Qt that allow you to pass messages between different components in your applications.

Signals are connected to slots which are functions (or methods) which will be run every time the signal fires. Many signals also transmit data, providing information about the state change or widget that fired them. The receiving slot can use this data to perform different actions in response to the same signal.

However, there is a limitation: the signal can only emit the data it was designed to. So for example, a QAction has a .triggered that fires when that particular action has been activated. The triggered signal emits a single piece of data -- the checked state of the action after being triggered.

For non-checkable actions, this value will always be False

The receiving function does not know whichQAction triggered it, or receiving any other data about it.

This is usually fine. You can tie a particular action to a unique function which does precisely what that action requires. Sometimes however you need the slot function to know more than that QAction is giving it. This could be the object the signal was triggered on, or some other associated metadata which your slot needs to perform the intended result of the signal.

This is a powerful way to extend or modify the built-in signals provided by Qt.

Intercepting the signal

Instead of connecting signal directly to the target function, youinstead use an intermediate function to intercept the signal, modify the signal data and forward that on to your actual slot function.

This slot function must accept the value sent by the signal (here the checked state) and then call the real slot, passing any additional data with the arguments.

Rather than defining this intermediate function, you can also achieve the same thing using a lambda function. As above, this accepts a single parameter checked and then calls the real slot.

python

In both examples the <additional args> can be replaced with anything you want to forward to your slot. In the example below we're forwarding the QAction object action to the receiving slot.

Our handle_trigger slot method will receive both the original checked value and the QAction object. Or receiving slot can look something like this

python

Below are a few examples using this approach to modify the data sent with the MainWindow.windowTitleChanged signal.

  • PyQt5
  • PySide2

The .setWindowTitle call at the end of the __init__ block changes the window title and triggers the .windowTitleChanged signal, which emits the new window title as a str. We've attached a series of intermediate slot functions (as lambda functions) which modify this signal and then call our custom slots with different parameters.

Running this produces the following output.

bash

The intermediate functions can be as simple or as complicated as you like -- as well as discarding/adding parameters, you can also perform lookups to modify signals to different values.

In the following example a checkbox signal Qt.Checked or Qt.Unchecked is modified by an intermediate slot into a bool value.

Pyqt Signal Slot Emit Meter

  • PyQt5
  • PySide2

In this example we've connected the .stateChange signal to result in two ways -- a) with a intermediate function which calls the .result method with True or False depending on the signal parameter, and b) with a dictionary lookup within an intermediate lambda.

Pyqt Signal Slot Emit Light

Pyqt

Running this code will output True or False to the command line each time the state is changed (once for each time we connect to the signal).

QCheckbox triggering 2 slots, with modified signal data

Trouble with loops

One of the most common reasons for wanting to connect signals in this way is when you're building a series of objects and connecting signals programmatically in a loop. Unfortunately then things aren't always so simple.

If you try and construct intercepted signals while looping over a variable, and want to pass the loop variable to the receiving slot, you'll hit a problem. For example, in the following code we create a series of buttons, and use a intermediate function to pass the buttons value (0-9) with the pressed signal.

  • PyQt5
  • PySide2

If you run this you'll see the problem -- no matter which button you click on you get the same number (9) shown on the label. Why 9? It's the last value of the loop.

The problem is the line lambda: self.button_pressed(a) where we pass a to the final button_pressed slot. In this context, a is bound to the loop.

Pyqt Signal Slot Emit Sensor

python

We are not passing the value of a when the button is created, but whatever value a has when the signal fires. Since the signal fires after the loop is completed -- we interact with the UI after it is created -- the value of a for every signal is the final value that a had in the loop: 9.

So clicking any of them will send 9 to button_pressed

The solution is to pass the value in as a (re-)named parameter. This binds the parameter to the value of a at that point in the loop, creating a new, un-connected variable. The loop continues, but the bound variable is not altered.

This ensures the correct value whenever it is called.

You don't have to rename the variable, you could also choose to use the same name for the bound value.

Pyqt Signal Slot Emit Generator

python

The important thing is to use named parameters. Putting this into a loop, it would look like this:

Running this now, you will see the expected behavior -- with the label updating to a number matching the button which is pressed.

The working code is as follows:

  • PyQt5
  • PySide2
Coments are closed
Scroll to top