Qt's signal and slot mechanism are a major feature of QT. It solves the synchronization trigger problem, that is, it provides a communication mechanism between different parts. The sending part only knows what signal to send, while the receiving part only knows what signal to accept, achieving perfect encapsulation.
This mechanism may be superficial for personal understanding. Make a record to sort out your ideas.
Qt provides a perfect mechanism, that is, you can define signals and slots between them and manage synchronization by yourself, that is, you can choose when to send signals. This is easy to handle. The communication problem between various components of a program is solved.
Class mainwindow: Public qmainwindow
{
Q_object
Public:
Explicit mainwindow (qwidget * parent = 0 );
Void initconnect (void );
~Mainwindow();
PRIVATE:
Ui: mainwindow * UI;
Qtimer * timer;
Win_qextserialport * mycom;
Signals:
Void valuechanged (void );
Private slots:
Void on_open_clicked (void );
};
The above is the definition of a class, which I copied from the QT creator.
The premise of using signals and slots is to inherit the qobject class or subclass, and then use the q_object macro. Signals and slots are not standard C ++ programs. Programs that use signals and slots need to compile signals and slots into standard C ++ programs through the metachille, then compiled by the C ++ compiler.
This signal and slot are clearly defined. They are defined in QT just like ordinary functions.
There is no difference. Of course, the signal function only defines the function and does not need to be implemented, because its function is to send a signal, no actual operations (sending signal mechanisms are encapsulated by QT ). The slot function must be implemented by yourself. The slot function is the action to be executed when you receive what you want.
Signals and slot functions can have parameters, but they must be consistent because they are connected through connect. They did the same for passing parameters. By the way, they haven't said how to contact them yet. Yes
Connect (this, signal (valuechanged (void), this, slot (on_open_clicked (void )));
This statement should be written to the class function. Depending on your needs, you can put it in the constructor. In this way, these two items are linked. It has four parameters, which is very simple. The pointer of the sending object, the sent signal, the pointer of the receiving object, and the active state after receiving the signal. Of course, this example uses the this pointer to indicate that all objects are local objects.
Now, everything is ready, who sends the signal, who receives the signal, and what is ready after the play, the next question is left, and what is the actual signal.
Okay. The QT signal and Slot Mechanism provide the code emit valuechanged () for sending signals. As long as this code is executed, the corresponding slot will be executed immediately. Now everything is clear. Haha
Also, some things make us confused. Sometimes, when using a component, such as Pushbutton, we can use the existing signal, such as clicked. It means that when you press the button with the mouse, the user sends the signal. Let's talk about the differences between signals, slots, and events. As mentioned above, the signal and slot solve the synchronization trigger problem in the same process, that is, the internal communication problem between different components. Events solve asynchronous triggering problems. For example, if you click the mouse, you are not sure about the events. These are QT event management mechanisms, today, we are going to understand the differences between signals, slots, and events.
In the MAI function
Return app.exe C ();
It is actually waiting for the response event.
We also talked about the mouse clicking problem. clicking the mouse is an event that triggers a clicked signal, which affects the corresponding slot.
Now, you can understand that if you want to implement your own asynchronous trigger mechanism, you can only define your own events. You can rest assured that QT provides a perfect mechanism for you to define events.
Finally, in QT, the signal and slot provide the synchronous trigger mechanism, and the event provides the asynchronous trigger mechanism.