Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.
Signals and Slots in Qt
This article provides an overview of Qt Signals and Slots. It is intended to be complimentary to the official Qt Reference
Signals and slots provide inter-object communication facilities. They are easy to understand and use, and are fully supported by Qt Creator. The signals and slots implementation smoothly extends C++’s syntax and takes full advantage of C++’s object-oriented features. Signals and slots are type-safe, can be overloaded or reimplemented, and may appear in the public, protected, or private sections of a class.
The signals and slots mechanism is a powerful inter-object communication mechanism that can be used to completely replace the crude callbacks and message maps used by legacy toolkits. Signals and slots are flexible, fully object-oriented, and implemented in C++.
To associate some code with a button using the old callback mechanism, it is necessary to pass a function pointer to the button. When the button is clicked, the function is then called. Old toolkits do not ensure that arguments of the correct type are given to the function when it is called, making crashes more likely. Another problem with the callback approach is that it tightly binds the GUI element to the functionality, making it difficult to develop classes independently.
Qt’s signals and slots mechanism is different. Qt widgets emit signals when events occur. For example, a button will emit a “clicked” signal when it is clicked. The programmer can choose to connect to a signal by creating a function (a “slot”) and calling the connect() function to relate the signal to the slot. Qt’s signals and slots mechanism does not require classes to have knowledge of each other, which makes it much easier to develop highly reusable classes. Since signals and slots are type-safe, type errors are reported as warnings and do not cause crashes to occur.
For example, if a Quit button’s clicked() signal is connected to the application’s quit() slot, a user’s click on Quit makes the application terminate. In code, this is written as
connect(button, SIGNAL(clicked()), qApp, SLOT(quit()));
Connections can be added or removed at any time during the execution of a Qt application, they can be set up so that they are executed when a signal is emitted or queued for later execution, and they can be made between objects in different threads.
Signals and slots model
Slots are almost identical to ordinary C++ member functions. They can be virtual; they can be overloaded; they can be public; protected, or private, they can be directly invoked like any other C++member functions; and their parameters can be of any types. The difference is that a slot can also be connected to a signal, in which case it is automatically called each time the signal is emitted.
The connect() statement looks like this:
connect(sender, SIGNAL(signal), receiver, SLOT(slot));
where sender and receiver are pointers to QObjects and where signal and slot are function signatures without parameter names. The SIGNAL() and SLOT() macros essentially convert their argument to a string.
The different possibilities to connect SIGNAL and SLOT are given below :
One signal can be connected to many slots
When the signal is emitted, the slots are called one after the other, in an unspecified order.
Many signals can be connected to the same slot
When either signal is emitted, the slot is called.
A signal can be connected to another signal
When the first signal is emitted, the second signal is emitted as well. Apart from that, signal–signal connections are indistinguishable from signal–slot connections.
Connections can be removed
This is rarely needed, because Qt automatically removes all connections involving an object when that object is deleted.
For more information about Signals and Slots see: http://doc.qt.nokia.com/latest/signalsandslots.html