Signal / Slot Mechanism#
The Signal/Slot mechanism is proper to Qt.
It hides an automatic implementation of callbacks registration.
Adding signal/slot handling#
Your class must inherit from
QObject
(through QWidget
for example) to be able to use the signal/slot mechanism.Also, the macro
Q_OBJECT
must be present in your class under its public:
attributes : 1#include <QWidget>
2#include <string>
3
4class MyEmitterClass : public QWidget
5{
6 public:
7 // Macro
8 Q_OBJECT
9 // Constructor
10 explicit MyEmitterClass() {}
11 // Dummy function to use signal
12 void sayHi() {emit sigSay("Hi");}
13
14 // Used signals
15 signals:
16 void sigSay(std::string str);
17}
18
19// ------------------------------------------
20
21#include <QWidget>
22#include <string>
23#include <iostream>
24
25class MyReceiverClass : public QWidget
26{
27 public:
28 // Macro
29 Q_OBJECT
30 // Constructor
31 explicit MyReceiverClass(MyEmitterClass& em)
32 {
33 // Link our slot to the object signal
34 if(!QObject::connect(&em,&MyEmitterClass::sigSay,
35 this,&MyReceiverClass::sayingSomething))
36 {
37 std::cout << "Connect error" << std::endl;
38 }
39 }
40
41 // Possible slots
42 private slots:
43 void sayingSomething(std::string str)
44 {
45 std::cout << str << std::endl;
46 }
47}
48
49// -----------------------------------------------
50
51// Main
52MyEmitterClass emitter;
53MyReceiverClass receiv(emitter);
54emitter.sayHi();
Signals#
Signals can be seen as a
doActionOnRegisteredCallbacks(args)
.They must be written under
signals:
tag.To distribute this signal, the syntax is such as
emit mySignal(args)
.The
emit
will call all the registered callbacks and exit only when those are done.Slots#
Slots are the functions called when the corresponding signal is emitted.
Those are always listed under
private slots:
tag.The function signature must match the signal signature.
They can be called like normal functions.
Linking both#
Linking the signals and slots require to know which object must be linked to which, and which function is linked to which signal :
QObject::connect(const QObject ptrSender, const char *ptrSignal, const QObject ptrSignal, const char *ptrSlot);
The function return
true
if the connection happened successfully.It is also possible to disconnect both by the same way :
QObject::disconnect(const QObject ptrSender, const char *ptrSignal, const QObject ptrSignal, const char *ptrSlot);
Note
Use this notation to connect signals and slots.
An older one exists, but does not ensure compatibilities and your program may crash on run.
Stop signals temporarily#
If, for any case, you need to stop the emitting of signals (you are modifying objects with temporarily NULL
pointers that should not be used while not replaced, and a signal would be emitted in that case) :
// some code with signal/slot working
{
const QSignalBlocker blocker(&myObjectToBlockSignalsFrom); // block events
// some code without signal/slot for this object
}
// some code with signal/slot working
The creation of the signal blocker will automatically inhibit emitting of any signals for targeted object.
When destroyed, it will automatically re-enable the signals.