Qt Signal Slot Lambda Parameter
- Qt Signal Slot Lambda Parameter Example
- Qt Signal Slot Lambda Parameter Finder
- Qt Signal Slot Lambda Parameters
- Qt Signal Slot Lambda Parameter Chart
The following sources have been used in making this material:
If your knowledge on signals and slots is rusty or they are a completely new concept to you, you can get started with the Programming 2 material on Qt basics.
Signals and slots¶
You need to use lambda or functools.partial to pass a argument to a slot. From functools import partial buttonbox = dialog.findChild(QDialogButtonBox, 'buttonBox') buttonBox.accepted.connect(partial(niveau, control, 1)) Information about functools.partial can be found here. But now we use a lambda for the receiver: Qt has no way to now that this lambda makes use of monitor. Even if monitor is deleted. Just like a classic signal-slot connection, if the context.
In Qt, signals and slots are a way for objects to communicate with eachother. A signal is emitted when some event occurs in the program. For example, when a button on the UI is pushed, the button (a QPushButton
widget) notifys the other widgets and objects about this by emitting a signal (emitclicked()
). A slot is a function called as a response to an emitted signal. For example, a slot findClicked()
is executed as a response to a button click.
All subclasses of QObject
or one of its subclasses can contain signals and slots. In practice, two things are required for the signal and slot mechanism to work. All classes that contain signals or slots must:
- mention the
Q_OBJECT
macro at the top of their declaration. - be derived from
QObject
Objects emit signals when their state changes in a way that may be interesting to other objects. The objects do not need to know anything about eachother. In fact, the only thing the object does is emit the signal. It does not know, care or need to know, if anyone receives the signals emitted and vice versa, a slot does not know if any signals are connected to it. Qt's signal and slot mechanism makes sure that if the signal is connected to a slot, the slot is automatically called with the signal's parameters.
One signal can be connected to as many slots as needed. If more than one slot is connected, they are called when the signal is emitted one after another in the same order they were connected. Similarly, more than one signal can be connected to the same slot. The slot is called when any of the signals are emitted. A signal can even be connected to another signal to emit the second signal as the first is emitted.
Signals¶
Qt's widgets have predefined signals. New signals can be defined in the class's signals
section of the interface.
Signals must not be implemented. The meta-object compiler moc generates them automatically. A signal can never have a return type and is always void
.
Slots¶
Slots are C++ functions that can be called normally as any other function. They can also be virtual if needed. In addition, signals can be connected to them. New slots are defined in the class's slots
section of the interface.
The programmer implements the slots (slots are C++ functions). Similarly to functions, slots can be either public or private.
Connecting¶
Signals and slots are connected with QObject::connect()
. The preferred syntax is functor based i.e.
where sender
is a pointer to the QObject
object emitting the signal and receiver
is a pointer to the QObject
object containing the slot. The second and fourth parameters are the signal and the slot respectively. The biggest benefits with this syntax is the compile-time type checking and the possibility to use lambdas as a part of the connect.

The other way to connect a signal to a slot is to use the SIGNAL and SLOT macros. This is typically referred as the old syntax.
where the SIGNAL and SLOT macros convert their parameters to strings. Type checking is left as a run time operation. However, you can connect C++ functions to QML function with this syntax.
Meta-object System¶
The Qt's meta-object system in Qt enables among other things signals and slots. It is based on three things:

# The QObject
class as the a base class for objects that can take advantage of the meta-object system.# The Q_OBJECT
macro at the beginning of the class declaration is used to enable meta-object features.# The Meta-Object Compiler (moc) supplies each of the QObject
subclasses with the necessary code to implement meta-object features. The moc tool reads a C++ source file. For the classes that contain the Q_OBJECTmacro
, it produces another C++ source file containing the meta-object code for each class. This generated source file is then typically compiled and linked with the class's implementation.
It is possible to use QObject
as a base class without the Q_OBJECT
macro. This means that signals, slots and the other meta-object features will not be available. For the meta-object system, a QObject
subclass without meta code is equivalent to its closest ancestor with meta-object code. Therefore, all subclasses of QObject should use the Q_OBJECT
macro even if they do not use signals, slots, and other meta-object system properties.
Qt5 alpha has been released. One of the features which I have been working on is a new syntax for signals and slot.This blog entry will present it.
Here is how you would connect a signal to a slot:
What really happens behind the scenes is that the SIGNAL
and SLOT
macros will convert their argument to a string. Then QObject::connect()
will compare those strings with the introspection data collected by the moc tool.
What's the problem with this syntax?
While working fine in general, we can identify some issues:
- No compile time check: All the checks are done at run-time by parsing the strings. That means if you do a typo in the name of the signal or the slot, it will compile but the connection will not be made, and you will only notice a warning in the standard output.
- Since it operates on the strings, the type names of the slot must match exactly the ones of the signal. And they also need to be the same in the header and in the connect statement. This means it won't work nicely if you want to use
typedef
or namespaces
In the upcoming Qt5, an alternative syntax exist. The former syntax will still work. But you can now also use this new way of connecting your signals to your slots:
Which one is the more beautiful is a matter of taste. One can quickly get used to the new syntax.
So apart from the aesthetic point of view, let us go over some of the things that it brings us:
Compile-time checking
You will get a compiler error if you misspelled the signal or slot name, or if the arguments of your slot do not match those from the signal.
This might save you some time while you are doing some re-factoring and change the name or arguments of signals or slots.
An effort has been made, using static_assert to get nice compile errors if the arguments do not match or of you miss a Q_OBJECT
Arguments automatic type conversion
Qt Signal Slot Lambda Parameter Example
Not only you can now use typedef
or namespaces properly, but you can also connect signalsto slots that take arguments of different types if an implicit conversion is possible
Qt Signal Slot Lambda Parameter Finder
In the following example, we connect a signal that has a QString
as a parameter to a slot that takes a QVariant
. It works because QVariant
has an implicit constructor that takes a QString
Connecting to any function
As you might have seen in the previous example, the slot was just declared as public
and not as slot
. Qt will indeed call directly the function pointer of the slot, andwill not need moc
introspection anymore. (It still needs it for the signal)
But what we can also do is connecting to any function or functor:
This can become very powerful when you associate that with boost or tr1::bind
.
C++11 lambda expressions
Everything documented here works with the plain old C++98. But if you use compiler that supportsC++11, I really recommend you to use some of the language's new features.Lambda expressions are supportedby at least MSVC 2010, GCC 4.5, clang 3.1. For the last two, you need to pass -std=c++0x asa flag.
You can then write code like:
This allows you to write asynchronous code very easily.
Qt Signal Slot Lambda Parameters
Update: Also have a look what other C++11 features Qt5 offers.
Qt Signal Slot Lambda Parameter Chart
It is time to try it out. Check out the alpha and start playing. Don't hesistate to report bugs.