Signals and slots
The Qt framework brings a flexible message exchange mechanism through three concepts: signals, slots, and connections:
- A
signal
is a message sent by an object - A
slot
is a function that will be called when this signal is triggered - The
connect
function specifies whichsignal
is linked to whichslot
Qt already provides signals and slots for its classes, which you can use in your application. For example, QPushButton
has a signal clicked()
, which will be triggered when the user clicks on the button. The QApplication
class has a slot quit()
function, which can be called when you want to terminate your application.
Here is why you will love Qt signals and slots:
- A slot remains an ordinary function, so you can call it yourself
- A single signal can be linked to different slots
- A single slot can be called by different linked signals
- A connection can be made between a signal and a slot from different objects, and even between objects living inside different threads!
Keep in mind that, to be able to connect a signal
to a slot
, their methods' signatures must match. The count, order, and type of arguments must be identical. Note that signals and slots never return values.
This is the syntax of a Qt connection:
connect(sender, &Sender::signalName, receiver, &Receiver::slotName);
The first test that we can do to use this wonderful mechanism is to connect an existing signal
with an existing slot
. We will add this connect call to the MainWindow
constructor:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->addTaskButton, &QPushButton::clicked, QApplication::instance(), &QApplication::quit); }
Let's analyze how a connection is done:
sender
: This is the object that will send the signal. In our example, it is theQPushButton
namedaddTaskButton
added from the UI designer.&Sender::signalName
: This is the pointer to the member signal function. Here, we want do something when the clicked signal is triggered.receiver
: This is the object that will receive and handle the signal. In our case, it is theQApplication
object created inmain.cpp
.&Receiver::slotName
: This is a pointer to one of the receiver'smember
slot functions. In this example, we use the built-inquit()
slot fromQapplication
, which will exit the application.
You can now compile and run this short example. You will terminate the application if you click on the addTaskButton
of your MainWindow
.
Tip
Qt tip
You can connect a signal to another signal. The second signal will be emitted when the first one is triggered.
Now that you know how to connect a signal to an existing slot, let's see how to declare and implement a custom addTask()
slot in our MainWindow
class. This slot will be called when the user clicks on ui->addTaskButton
.
This is the updated MainWindow.h
:
class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: void addTask(); private: Ui::MainWindow *ui; };
Qt uses a specific slot
keyword to identify slots. Since a slot is a function, you can always adjust the visibility (public
, protected
or private
) depending on your needs.
Add this slot implementation in the MainWindow.cpp
file:
void MainWindow::addTask() { qDebug() << "User clicked on the button!"; }
Qt provides an efficient way of displaying debug information with the QDebug
class. An easy way to obtain a QDebug
object is to call the qDebug()
function. Then, you can use the stream operator to send your debug information.
Update the top of the file like this:
#include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->addTaskButton, &QPushButton::clicked, this, &MainWindow::addTask); }
Since we now use qDebug()
in out slot, we must include <QDebug>
. The updated connect now calls our custom slot instead of quitting the application.
Build and run the application. If you click on the button, you will see your debug message inside the Qt Creator Application Output
tab.