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. Together, signals and slots make up a powerful component programming mechanism. (This will emit the second signal immediately whenever the first is emitted.) It is even possible to connect a signal directly to another signal. 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. This ensures that truly independent components can be created with Qt. 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. Slots can be used for receiving signals, but they are also normal member functions. This is true information encapsulation, and ensures that the object can be used as a software component. It does not know or care whether anything is receiving the signals it emits. This is all the object does to communicate. Signals are emitted by objects when they change their state in a way that may be interesting to other objects. They are completely type safe.Īll classes that inherit from QObject or one of its subclasses (e.g., QWidget) can contain signals and slots. Signals and slots can take any number of arguments of any type. 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 are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal. The string-based SIGNAL and SLOT syntax will detect type mismatches at runtime.
(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 signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. 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. A slot is a function that is called in response to a particular signal. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A signal is emitted when a particular event occurs. In Qt, we have an alternative to the callback technique: We use signals and slots. 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. The processing function then calls the callback when appropriate. 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.
Other toolkits achieve this kind of communication using callbacks. For example, if a user clicks a Close button, we probably want the window's close() function to be called. More generally, we want objects of any kind to be able to communicate with one another. In GUI programming, when we change one widget, we often want another widget to be notified. Signals and slots are made possible by Qt's meta-object system. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. Signals and slots are used for communication between objects.