Signals 和 Slots 用于对象间的通信（communication between objects）。这种机制是Qt区别于其他框架的主要特点。这种机制是靠Qt的meta-object system实现的。 介绍. 很多框架使用callback技术（MFC，CVI等）。一个 callback 其实就是一个函数指针，但是Qt认为callback并不直观，而且在. 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. Callback.onWiFiDeviceDiscovered(pDevInfo, x); You would emit a signal: emit onWiFiDeviceDiscovered(pDevInfo, x); And you would use QObject::connect to connect these signals to your actual receivers (slots) on the other end. Be aware that emitting a signal is more expensive than calling a virtual function.
Envision you've decided to go solo and start an indie game studio to fulfil your lifelong dream - to design and create a turn-based strategy game. You begin your endeavour by outlining the different components and sketch on the overall architecture of the game. The game will have some units which can be moved and controlled by using the mouse. There will be an AI component which will perhaps kick in after the units have completed their movements. The AI will then decide how the enemies should respond.
In other words, a sequence of actions need to happen: the mouse click, then the movement of units and then the response of the enemies. However, you probably don't want the mouse logic to directly depend on the unit-classes; it will be used for much more. For the same reason the unit-classes shouldn't need to rely on the AI nor the enemy logic; possibly the same AI will be used for many different enemies. I'm sure you're a great developer so you're aiming to decouple these different components. Nonetheless, the components need to communicate with each other and you need some kind of callback-mechanism. And this, ladies and gentlemen, this is where Qt's signals and slots comes to the rescue.
This is the third post in the series 'Crash course in Qt for C++ developers' covering the signals and slots mechanism. The other topics are listed below.
- Signals and slots - communication between objects
- Qt Quick/QML example
- Qt Widgets example
- Tooling, e.g. Qt Creator
- Remaining good-to-know topics
- Where to go from here?
The most common usage of the signals and slots mechanism is for inter-object communication and has been around since the birth of Qt. It's a system where an object broadcasts a signal with data and any listeners, including the sender itself, can subscribe to that signal. The listeners then redirect the data to a member function, or what's so called the slot, where it's then processed.
You might wonder how this system is different from using a standard callback-mechanism used in many other GUI tools. One benefit of Qt's solution is that the sender is not dependent on the listener, it's just firing off the signal. In a callback mechanism the sender usually needs to know which listeners to notify. Another benefit, in contrast to many other callback implementations, is that the signals and slots functions are type safe.
OK - enough text, let's look at some code.
Let's start with signals. In order to enable signals for a class, it has to inherit from
QObject and use the
Q_OBJECT macro. We'll define a signal called
signalName with three arguments. The arguments are used to pass in data, which will later be available in the slots.
signals keyword is essentially a simple macro defined to
public but is also used by the MOC (remember from the previous post?) to generate the implementation of the member function
signalName(). The implementation can be found in the generated file moc_mysender.cpp. The signal is declared similarly to a regular function, except for a constraint on the return value - it has to be
void. To emit the signal, we'll only call the function with the addition of appending
emit is a macro defined to do... nothing. It's only used for documentation and readability purposes. You could omit it: the code would compile fine and the outcome would be the same, but again, we want to be explicit that it's a signal. Also, notice that we don't need to depend on any of the listeners, we're only so far just emitting a signal. So how would an object listen to it?
A listener object will redirect the signal to one of its slots. However, slots are not limited to member functions but can also consist of lambdas, non-member functions and functors. The arguably most common use is to define the slot within an object, so let's start by creating a new class
MyReceiver which has a slot called
Similarly to the signal object, the receiver class needs to inherit from
QObject and also use the
Q_OBJECT macro. Furthermore,
slots is another macro declared to do nothing, but in contrast to
emit it's used by the MOC to generate introspection code. Slots-function can be specified as public, protected and private; Signals are always public.
Did I mention that the system is type safe? The signal's and slot's signature must find a match in order to link them: the arguments must be of the same types and qualifiers and declared in the same order. So how do we connect our signal to our slot?
We'll use the static function QObject::connect():
QObject::connect() takes five arguments, the last one has a default value and will be ignored for now. From left to right:
- Start with the object which emits the signal - the sender.
- Next we have a pointer to a member function - the signal.
- Hook it up to the listener - the receiver.
- Last, we have another pointer to a member function - the slot.
Now, if we emit out signal, the slot will be notified and we get the following output:
Important data that will be sent to all listeners 42 1.618033
You may connect many different signals to the same slot, or use the same signal for many different slots. It's your choice - the system is very flexible. As mentioned, the system even allows to connect functions and lambdas directly, without using a listener, e.g:
The connection is automatically disconnected if either the sender or the receiver becomes deleted. However, as you might have guessed it's also possible to manually disconnect it by using disconnect:
Heads-up when capturing data in a lambda
Be extra careful when capturing data using lambdas or functors as only the sender will automatically control the lifetime of the connection. Consider the following case:
data has been deleted prior to emitting the signal? The connection might still be alive (if it hasn't been manually disconnected) and we'll have a naughty bug in our application. However, Qt provides another overload of the
connect() function where it's possible to provide a context object. This is used like so:
Notice the third argument. We're now also providing the same captured object as a context object. If the context object is deleted, the connection will automatically disconnect. Note that the context object has to inherit from
QObject for this to work.
We've now come to an end of the basics. There should be enough information here to cover the most common cases. But down the development road you might hit some issues and will need some additional information in the future. Perhaps the next section will help you at those times.
Tips and tricks
- In case you have multiple overloads of a signals or slots you'll need to perform a cast within the
Although it should be avoided, it's possible to delete a listener (or schedule it for deletion) within its slot by using deletelater().
There are several different types of connections. The type is defined by the fifth argument in the
connect()-function. The type is an enum called Qt::ConnectionType and is defaulted to
Qt::AutoConnection. In a single threaded application, the default behaviour is for the signal to directly notify the listeners. However, in a multi threaded application, usually the connections between the threads are queued up on each respective event loop. Threads are outside the scope of this series but is very well documented by Qt.
It's possible to connect a signal directly to another object's signal. This can be used, for example, to forward signals:
- You might find a different syntax that's used for the
connect()function by other tutorials, blogs or even the Qt documentation, see code below. This style was the main syntax used up until Qt5. The syntax shown in this post provides several benefits over the old style. Personally, I believe the best advantage is that it allows compile-time checking of the signals and slots, which wasn't previously possible. However, in some cases the old syntax must be used, for example when connecting C++ functions to QML functions.
Do you remember the
slotskeyword above (when defining the slot-functions)? It's actually only necessary to define when using the old
connect()syntax mentioned in the previous point. The type checking for the old syntax is done at run-time by using type introspection. The MOC generates the introspection code, but only if the
slotskeyword is defined.
If you need to know which signals and slots are connected to a specific object at a certain point in time, you'll find QObject::dumpObjectInfo() very helpful. It's especially useful when debugging since it will output all the inbound and outbound signals for the object.
Back to the game architecture
You might now have a better understanding on how signals and slots can be used to separate the different game components. For example, let's say that we define four components which should be decoupled: the MouseComponent, the UnitComponent, the AIComponent and lastly the EnemyComponent. Furthermore, we'll use signals in each component to communicate and notify the other components. However to solve the separation, we'll have to introduce another component, the MainController, which will be used to create all the connections and define the game flow. The MainController will obviously need to depend on the different components, however the components are now well isolated from each other and we've achieved our goal. Do you see how this can be used in a GUI application to separate the visuals and user interaction from the actual logic?
Lastly, if you're interested in reading more about the inner-works of the signals and slots mechanism woboq.com has written a great blog series which has got you covered. Let me know if something is unclear or perhaps if I missed something out.
See you next time!
In GUI programming, when we change one widget, we often want another widget to be notified. More generally, we want objects of any kind to be able to communicate with one another. For example, if a user clicks a Close button, we probably want the window's close() function to be called.
Other toolkits achieve this kind of communication using callbacks. A callback is a pointer to a function, so if you want a processing function to notify you about some event you pass a pointer to another function (the callback) to the processing function. The processing function then calls the callback when appropriate. While successful frameworks using this method do exist, callbacks can be unintuitive and may suffer from problems in ensuring the type-correctness of callback arguments.
Signals and Slots
In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal. Qt's widgets have many pre-defined slots, but it is common practice to subclass widgets and add your own slots so that you can handle the signals that you are interested in.
The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) Since the signatures are compatible, the compiler can help us detect type mismatches when using the function pointer-based syntax. The string-based SIGNAL and SLOT syntax will detect type mismatches at runtime. 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. They are completely type safe.
All classes that inherit from QObject or one of its subclasses (e.g., QWidget) can contain signals and slots. Signals are emitted by objects when they change their state in a way that may be interesting to other objects. This is all the object does to communicate. It does not know or care whether anything is receiving the signals it emits. This is true information encapsulation, and ensures that the object can be used as a software component.
Slots can be used for receiving signals, but they are also normal member functions. Just as an object does not know if anything receives its signals, a slot does not know if it has any signals connected to it. This ensures that truly independent components can be created with Qt.
You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)
Together, signals and slots make up a powerful component programming mechanism.
Signals are emitted by an object when its internal state has changed in some way that might be interesting to the object's client or owner. Signals are public access functions and can be emitted from anywhere, but we recommend to only emit them from the class that defines the signal and its subclasses.
When a signal is emitted, the slots connected to it are usually executed immediately, just like a normal function call. When this happens, the signals and slots mechanism is totally independent of any GUI event loop. Execution of the code following the
emit statement will occur once all slots have returned. The situation is slightly different when using queued connections; in such a case, the code following the
emit keyword will continue immediately, and the slots will be executed later.
If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.
Signals are automatically generated by the moc and must not be implemented in the
.cpp file. They can never have return types (i.e. use
A note about arguments: Our experience shows that signals and slots are more reusable if they do not use special types. If QScrollBar::valueChanged() were to use a special type such as the hypothetical QScrollBar::Range, it could only be connected to slots designed specifically for QScrollBar. Connecting different input widgets together would be impossible.
A slot is called when a signal connected to it is emitted. Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them.
Since slots are normal member functions, they follow the normal C++ rules when called directly. However, as slots, they can be invoked by any component, regardless of its access level, via a signal-slot connection. This means that a signal emitted from an instance of an arbitrary class can cause a private slot to be invoked in an instance of an unrelated class.
You can also define slots to be virtual, which we have found quite useful in practice.
Compared to callbacks, signals and slots are slightly slower because of the increased flexibility they provide, although the difference for real applications is insignificant. In general, emitting a signal that is connected to some slots, is approximately ten times slower than calling the receivers directly, with non-virtual function calls. This is the overhead required to locate the connection object, to safely iterate over all connections (i.e. checking that subsequent receivers have not been destroyed during the emission), and to marshall any parameters in a generic fashion. While ten non-virtual function calls may sound like a lot, it's much less overhead than any
delete operation, for example. As soon as you perform a string, vector or list operation that behind the scene requires
delete, the signals and slots overhead is only responsible for a very small proportion of the complete function call costs. The same is true whenever you do a system call in a slot; or indirectly call more than ten functions. The simplicity and flexibility of the signals and slots mechanism is well worth the overhead, which your users won't even notice.
Note that other libraries that define variables called
slots may cause compiler warnings and errors when compiled alongside a Qt-based application. To solve this problem,
#undef the offending preprocessor symbol.
Qt Signal Slots Vs Callbacks
A Small Example
A minimal C++ class declaration might read:
A small QObject-based class might read:
The QObject-based version has the same internal state, and provides public methods to access the state, but in addition it has support for component programming using signals and slots. This class can tell the outside world that its state has changed by emitting a signal,
valueChanged(), and it has a slot which other objects can send signals to.
All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.
Slots are implemented by the application programmer. Here is a possible implementation of the
emit line emits the signal
valueChanged() from the object, with the new value as argument.
In the following code snippet, we create two
Counter objects and connect the first object's
valueChanged() signal to the second object's
setValue() slot using QObject::connect():
a emit a
valueChanged(12) signal, which
b will receive in its
setValue() slot, i.e.
b.setValue(12) is called. Then
b emits the same
valueChanged() signal, but since no slot has been connected to
valueChanged() signal, the signal is ignored.
Note that the
setValue() function sets the value and emits the signal only if
value != m_value. This prevents infinite looping in the case of cyclic connections (e.g., if
b.valueChanged() were connected to
By default, for every connection you make, a signal is emitted; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnectiontype, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return
This example illustrates that objects can work together without needing to know any information about each other. To enable this, the objects only need to be connected together, and this can be achieved with some simple QObject::connect() function calls, or with uic's automatic connections feature.
A Real Example
The following is an example of the header of a simple widget class without member functions. The purpose is to show how you can utilize signals and slots in your own applications.
LcdNumber inherits QObject, which has most of the signal-slot knowledge, via QFrame and QWidget. It is somewhat similar to the built-in QLCDNumber widget.
The Q_OBJECT macro is expanded by the preprocessor to declare several member functions that are implemented by the
moc; if you get compiler errors along the lines of 'undefined reference to vtable for
LcdNumber', you have probably forgotten to run the moc or to include the moc output in the link command.
After the class constructor and
public members, we declare the class
LcdNumber class emits a signal,
overflow(), when it is asked to show an impossible value.
If you don't care about overflow, or you know that overflow cannot occur, you can ignore the
overflow() signal, i.e. don't connect it to any slot.
If on the other hand you want to call two different error functions when the number overflows, simply connect the signal to two different slots. Qt will call both (in the order they were connected).
A slot is a receiving function used to get information about state changes in other widgets.
LcdNumber uses it, as the code above indicates, to set the displayed number. Since
display() is part of the class's interface with the rest of the program, the slot is public.
Several of the example programs connect the valueChanged() signal of a QScrollBar to the
display() slot, so the LCD number continuously shows the value of the scroll bar.
display() is overloaded; Qt will select the appropriate version when you connect a signal to the slot. With callbacks, you'd have to find five different names and keep track of the types yourself.
Signals And Slots With Default Arguments
The signatures of signals and slots may contain arguments, and the arguments can have default values. Consider QObject::destroyed():
When a QObject is deleted, it emits this QObject::destroyed() signal. We want to catch this signal, wherever we might have a dangling reference to the deleted QObject, so we can clean it up. A suitable slot signature might be:
To connect the signal to the slot, we use QObject::connect(). There are several ways to connect signal and slots. The first is to use function pointers:
There are several advantages to using QObject::connect() with function pointers. First, it allows the compiler to check that the signal's arguments are compatible with the slot's arguments. Arguments can also be implicitly converted by the compiler, if needed.
You can also connect to functors or C++11 lambdas:
In both these cases, we provide this as context in the call to connect(). The context object provides information about in which thread the receiver should be executed. This is important, as providing the context ensures that the receiver is executed in the context thread.
The lambda will be disconnected when the sender or context is destroyed. You should take care that any objects used inside the functor are still alive when the signal is emitted.
The other way to connect a signal to a slot is to use QObject::connect() and the
SLOT macros. The rule about whether to include arguments or not in the
SLOT() macros, if the arguments have default values, is that the signature passed to the
SIGNAL() macro must not have fewer arguments than the signature passed to the
All of these would work:
But this one won't work:
...because the slot will be expecting a QObject that the signal will not send. This connection will report a runtime error.
Note that signal and slot arguments are not checked by the compiler when using this QObject::connect() overload.
Qt Signal Slot Callback
Advanced Signals and Slots Usage
For cases where you may require information on the sender of the signal, Qt provides the QObject::sender() function, which returns a pointer to the object that sent the signal.
Lambda expressions are a convenient way to pass custom arguments to a slot:
Using Qt with 3rd Party Signals and Slots
It is possible to use Qt with a 3rd party signal/slot mechanism. You can even use both mechanisms in the same project. Just add the following line to your qmake project (.pro) file.
It tells Qt not to define the moc keywords
emit, because these names will be used by a 3rd party library, e.g. Boost. Then to continue using Qt signals and slots with the
no_keywords flag, simply replace all uses of the Qt moc keywords in your sources with the corresponding Qt macros Q_SIGNALS (or Q_SIGNAL), Q_SLOTS (or Q_SLOT), and Q_EMIT.
See also QLCDNumber, QObject::connect(), Digital Clock Example, Tetrix Example, Meta-Object System, and Qt's Property System.
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.