Qt signal and slot mechanism

QT provides a signal and slot mechanism to complete the interface operation response, and the signal and slot mechanism is to complete the communication mechanism between any two QT objects.

Signal (Signal) is the event that is emitted under specific circumstances. For example, the most common signal of PushButton is the clicked() signal emitted when the mouse is clicked. The most common signal of a ComboBox is the CurrentIndexChanged() emitted when the selected list item changes. Signal. The main content of GUI programming is the response to the signals of the various components on the interface. It is only necessary to know which signals are emitted under what circumstances, and to respond and process these signals reasonably.

Slots are functions that respond to signals. A slot is a function, which is the same as a general C++ function. It can be defined in any part of the class (public, private or protected), can have any parameters, and can also be called directly. The difference between a slot function and a general function is that a slot function can be associated with a signal, and when the signal is emitted, the associated slot function is automatically executed.

The connection method of signal and slot mechanism

  • basic format
connect(Object1,SIGNAL(signal1),Object2,SLOT(slot2));
  • A signal can be connected to another signal
connect(Object1,SIGNAL(signal1),Object2,SIGNAL(signal2));

Object1's signal 1 sending can trigger Object2's signal 2 sending

  • The same signal can be connected to multiple slots
connect(Object1,SIGNAL(signal1),Object2,SLOT(slot2));
connect(Object1,SIGNAL(signal1),Object3,SLOT(slot3));
  • The same slot can respond to multiple signals
connect(Object2,SIGNAL(signal2),Object1,SLOT(slot1));
connect(Object3,SIGNAL(signal3),Object1,SLOT(slot1));

SIGNAL() and SLOT() are two macros defined by Qt that return a C-style string (const char*) of its argument. Therefore, the following two statements that associate signals and slots are equivalent:

connect(buttoon, SIGNAL(clicked()), this, SLOT(showArea()));
connect(buttoon, "clicked()", this, "showArea()");

Advantages of the signal and slot mechanism

  • type safety

The signatures of the associated signals and slots must be identical, that is, the parameter types and number of parameters of the signal are the same as the parameter types and number of parameters of the slot receiving the signal. However, the number of parameters of the slot can be less than the number of parameters of the signal, but the missing parameters must be the last one or more of the signal parameters.

  • loosely coupled

The signal and slot mechanism weakens the coupling of QT objects. The object that fires the signal doesn't need to know which objects' slots need to correspond to it, and it doesn't care whether the signal is received or not. The same slot does not know which signals are associated with itself. Once the signal and slot are associated, Qt ensures that the appropriate slot is called.

If a class wants to support signals and slots, it must inherit from QObject or QObject's class. Signals and slots do not support the use of templates.

Efficiency of the signals and slots mechanism

The signal and slot mechanism enhances the flexibility of communication between objects, which also loses some performance. The main reasons are as follows, but this performance loss is worthwhile in comparison.

  • Need to locate the object receiving the signal
  • Safely traverse all associations (one signal to many slots)
  • Marshalling and unmarshalling passed arguments
  • When multithreading, signals may need to be queued

Guess you like

Origin blog.csdn.net/m0_45463480/article/details/130439379