The Qt framework offers a flexible message-exchange mechanism that is composed of three concepts:
- signal is a message sent by an object
- slot is a function that will be called when this signal is triggered
- The connect function specifies which signal is linked to which slot
Qt already provides signals and slots for its classes, which you can use in your application. For example, QPushButton has signal clicked(), which will be triggered when the user clicks on the button. Another example: 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: Object that will send the signal. In our example, the QPushButton named addTaskButton is added from the UI designer.
- &Sender::signalName: Pointer to the member signal function. Here, we want do something when the clicked signal is triggered.
- receiver: Object that will receive and handle the signal. In our case, it is the QApplication object created in main.cpp.
- &Receiver::slotName: Pointer to one of the receiver's member slot functions. In this example, we use the built-in quit() slot from QApplication, which will exit the application.
You can now compile and run this short example. You will terminate the application if you click on addTaskButton of your MainWindow.
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.
The following 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 slots keyword to identify slots. Since a slot is a function, you can always adjust the visibility (public, protected, or private) depending on your needs.
We will now 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 the 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 the following:
#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 our 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 Application Output Qt Creator tab.