The signal and slot are an advanced interface used for communication between objects. It is the core feature of QT and an important part of distinguishing QT from other toolkit.
All classes derived from qobject or its subclass (such as qwidget) can contain signals and slots. When an object changes its state, the signal is sent out by the object (emit). This is all the things the object has to do. It does not know who is receiving the signal at the other end. This is the true information encapsulation, which ensures that the object is used as a real software component. Slots are used to receive signals, but they are common object member functions. A slot does not know whether any signal is connected to itself. Moreover, the object does not understand the specific communication mechanism. You can connect many signals to a single slot, or connect a single signal to many slots, or even connect one signal to another. If multiple slots are associated with a specific signal, these slots will be executed one by one when the signal is sent, however, their execution order will be random and uncertain. We cannot artificially specify which is executed first and which is then executed.
Signal:
It can be some signals that come with the control, such as Pushbutton's clicked () signal. You can also define it in the header file:
signals: void mysignal();
Slot:It can be a built-in slot of the object, such as qapplication's quit. You can also define it in the header file:
slots: void myslot();
Correlation between signals and slots:
Call the connect function of the qobject object to associate the signal of an object with the slot function of another object. In this way, when the transmitter emits a signal, the receiver's slot function is called. The function is defined as follows:
bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * member ) [static]
This function is used to associate the signal in the transmitter sender object with the member slot function in the receiver. The macro signal () of QT must be used when the signal is specified, and the macro slot () must be used when the slot function is specified (). If the initiator and receiver belong to the same object, the receiver parameter can be omitted in the Connect call.
As shown in the following figure, the button can exit:
QPushButton *button=new QPushButton(this); connect(button,SIGNAL(clicked()),qApp,SLOT(quit()));
Here, we associate the clicked signal of the button with the exit quit () slot of the qapp. The signals and slots here are self-contained in the class.
When the button is clicked, The quit () method is triggered, and quit () calls exit () to exit the application.
You can also drag Pushbutton into the UI, right-click the button, and select to go To the slot
Then select clicked:
Compile the corresponding processing function below:
void Dialog::on_pushButton_clicked(){ //qApp->quit(); qApp->exit();}
In the response function, you can call sender () to obtain the object for sending signals. For example:
QPushButton *btn=(QPushButton*)sender();btn->setText("adfdfds");
In this way, the principle is the same.