Qt Connect Slot Base Class
This page describes the use of signals and slots in Qt for Python.The emphasis is on illustrating the use of so-called new-style signals and slots, although the traditional syntax is also given as a reference.
The main goal of this new-style is to provide a more Pythonic syntax to Python programmers.
- 2New syntax: Signal() and Slot()
Traditional syntax: SIGNAL () and SLOT()
QtCore.SIGNAL() and QtCore.SLOT() macros allow Python to interface with Qt signal and slot delivery mechanisms.This is the old way of using signals and slots.
The example below uses the well known clicked signal from a QPushButton.The connect method has a non python-friendly syntax.It is necessary to inform the object, its signal (via macro) and a slot to be connected to.
Signals and slots are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal. Qt's signals and slots mechanism ensures that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time. Signals and slots can take any number of arguments of any type.
- It will output 'derived', that is, the base class's connect option, connect the signal to the derived class's slot. But apparently the arguments I passed to the connect function is the base class's 'this' pointer and the base class's 'slot1' function pointer. It's a bit confused to me. Can someone explain it more?
- While being better in many regards, the new connection syntax in Qt5 has one big weakness: Connecting overloaded signals and slots. In order to let the compiler resolve the overloads we need to use staticcasts to member function pointers, or (starting in Qt 5.7) qOverload and friends.
Qt Connect Slot Base Classic
New syntax: Signal() and Slot()
The new-style uses a different syntax to create and to connect signals and slots.The previous example could be rewritten as:
Using QtCore.Signal()
Qt Connect To Slot In Base Class
Signals can be defined using the QtCore.Signal() class.Python types and C types can be passed as parameters to it.If you need to overload it just pass the types as tuples or lists.
In addition to that, it can receive also a named argument name that defines the signal name.If nothing is passed as name then the new signal will have the same name as the variable that it is being assigned to.
The Examples section below has a collection of examples on the use of QtCore.Signal().
Note: Signals should be defined only within classes inheriting from QObject.This way the signal information is added to the class QMetaObject structure.
Using QtCore.Slot()
Slots are assigned and overloaded using the decorator QtCore.Slot().Again, to define a signature just pass the types like the QtCore.Signal() class.Unlike the Signal() class, to overload a function, you don't pass every variation as tuple or list.Instead, you have to define a new decorator for every different signature.The examples section below will make it clearer.
Another difference is about its keywords.Slot() accepts a name and a result.The result keyword defines the type that will be returned and can be a C or Python type.name behaves the same way as in Signal().If nothing is passed as name then the new slot will have the same name as the function that is being decorated.
Examples
The examples below illustrate how to define and connect signals and slots in PySide2.Both basic connections and more complex examples are given.
- Hello World example: the basic example, showing how to connect a signal to a slot without any parameters.
- Next, some arguments are added. This is a modified Hello World version. Some arguments are added to the slot and a new signal is created.
- Add some overloads. A small modification of the previous example, now with overloaded decorators.
- An example with slot overloads and more complicated signal connections and emissions (note that when passing arguments to a signal you use '[]'):
- An example of an object method emitting a signal:
- An example of a signal emitted from another QThread:
- Signals are runtime objects owned by instances, they are not class attributes:
- PyQt Tutorial
- PyQt Useful Resources
- Selected Reading
Unlike a console mode application, which is executed in a sequential manner, a GUI based application is event driven. Functions or methods are executed in response to user’s actions like clicking on a button, selecting an item from a collection or a mouse click etc., called events.
Widgets used to build the GUI interface act as the source of such events. 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.
In PyQt, connection between a signal and a slot can be achieved in different ways. Following are most commonly used techniques −
A more convenient way to call a slot_function, when a signal is emitted by a widget is as follows −
Suppose if a function is to be called when a button is clicked. Here, the clicked signal is to be connected to a callable function. It can be achieved in any of the following two techniques −
or
Example
In the following example, two QPushButton objects (b1 and b2) are added in QDialog window. We want to call functions b1_clicked() and b2_clicked() on clicking b1 and b2 respectively.
When b1 is clicked, the clicked() signal is connected to b1_clicked() function
When b2 is clicked, the clicked() signal is connected to b2_clicked() function
Example
The above code produces the following output −