Signal and slot principle in Qt

1. What is the signal and slot mechanism?

  ​ Signals and slots (Signal & Slot) are used to communicate between two objects. In layman's terms, one object sends out a signal, and the other object performs the corresponding action (for example, in a running game, the referee makes a gunshot signal, the athletes start running).

  Signal (Signal): It is equivalent to sending out an event notification.

  Slot: A slot is actually a function, a series of instructions executed in response to a signal.

  Connect: The process of this connection, that is, we specify what kind of signal to execute what kind of slot function.

  Only classes derived from QOBject in Qt support signals and slots. Mainly divided into two categories: 1. Qt class comes with it; 2. User-defined.

2. Signal

  For the signals that come with Qt, generally you only need to know which signals are there, and use them directly. Several common signals in Qt are as follows:

void clicked(bool checked = false)

void pressed()

void released()

void toggled(bool check)

  If you want to use a custom signal, you must first write a new class and let it inherit some standard classes of Qt. If the class we write ourselves wants to use the signal slot mechanism in Qt, the following conditions must be met:   1. This class must be derived from the QObject class or its subclass   2. Add the Q_OBJECT macro to the header file defining the class   Requirements:   1. The signal is a member function of the class   2. The return value is void type   3. The name of the signal can be Specify according to the actual situation   4. Parameters can be specified at will, and the signal also supports overloading void signal_test(int, int, char);   5. Signals need to be declared with the keywords keywords, which are similar to keywords such as public   6. Signal functions Just declare   7. Send a custom signal in the program: the essence of sending a signal is to call the signal function
  
  

  
  
  
  
  
  
  

  When we write down the emit signal code, the slot connected to this signal will be called, so how does this call happen? Let us unravel the mysteries one by one.

  Let's look at an example code:

class ZMytestObj : public QObject
{
    
    
Q_OBJECT
signals:
void sigMenuClicked();
void sigBtnClicked();
};

  The code of the compiler after preprocessing is as follows:


// SIGNAL 0
void ZMytestObj::sigMenuClicked()
{
    
    
QMetaObject::activate(this, &staticMetaObject, 0, 0);
}
 
// SIGNAL 1
void ZMytestObj::sigBtnClicked()
{
    
    
QMetaObject::activate(this, &staticMetaObject, 1, 0);

So the signal function can also be written in this way to define.

3. Slot function

  The characteristics are:    1. The return value is void    2. There can be parameters. The type and number of parameters need to correspond to the corresponding signals. The number of parameters can be less than the signal, but not more. void slot_test(int); and the previous void signal_test (int, int, char) corresponds to
  
  

4. Connect the signal slot

  connect function

 //QT4
 connect(m_pPushBtn, SIGNAL(clicked(bool)), &signalTest, SLOT(slots_TestSlot1()));

 //QT5
 connect(m_pPushBtn, &QPushButton::clicked, &signalTest, &CSignalTest::slots_TestSlot1);

   Note:    1. A signal can be connected to multiple slot functions.    After the signal is triggered, all slot functions are called, and the calling order is uncertain.
  
     

      2. A slot function can be    triggered by multiple signals connected to one of the signals, the slot function will be called
     

      3.connect can connect signal and signal    connect(m_pPushBtn, SIGNAL(clicked(bool)), &signalTest, SIGNAL(signals_TestSignls()));
     

Guess you like

Origin blog.csdn.net/AAAA202012/article/details/130580433