Signal and slot functions in QT

A signal and slot mechanism
1 Concept
    Signal and slot is a communication mechanism defined in Qt to realize the interaction between objects. When an object changes, a signal will be sent. This signal can be received by other objects. A specified member function (slot function) will be executed.

Illustration:

2 Definition
1) The class containing the signal or slot must be a subclass of QObject
2) The signal is marked with "signals:", the signal function only needs to be declared , and the definition cannot be written
3) The slot is marked with "public slots:", the slot function can be A certain signal establishes a connection, and triggers the execution of the slot function through a certain signal; in addition, the slot function can also be used as an ordinary member function, directly called
4) the class containing the signal or slot, the macro "Q_OBJECT" needs to be added in the front, and the project will be built in the future , It will call moc (meta-object compiler) to restore the signal or slot of the syntax extension to standard C ++ code.

Code implementation format:
      class XX: public QObject {
          Q_OBJECT // moc
      signals:
          void sigFunc (void); // Signal function
      public slots:
          void slotFunc (void) {...} // slot function
      };    


3 Signal and slot connection
  QObject :: connect (
      const QObject * sender, // Signal sending object, can be all subclass types of QObject 
      const char * signal, // Signal function
      const QObject * receiver, // Signal receiving object, can be QObject all subclass types
      const char * method); // Slot function


  Note :    
      SIGNAL (signal function (parameter table)) // Convert signal function to const char *
      SLOT (slot function (parameter table)) // Convert slot function to const char *
Case: Create a Qt application, which contains One label and one button to close the label by clicking the button.

4 The signal and slot connection syntax requirements
1) The signal and slot parameters must be consistent
    QObject :: connect (A, SIGNAL (sigFunc (int)), B, SLOT (slotFunc (int))); // ok
    QObject :: connect ( A, SIGNAL (sigFunc (void)), B, SLOT (slotFunc (int))); // error

 The implementation of the slot function in the old version of Qt:

     connect(A,SIGNAL(sigFunc(int)),B,SLOT(slotFunc(int)));

 The implementation of the slot function in the new version of Qt:

     connect(A,Signal1,B,Slot1);

The characteristics of the old version of the slot function function: the advantages of the parameters are intuitive and the   disadvantages are not type detection, so it is not recommended to use


2) Can carry default parameters
    class B {
        Q_OBJECT
    public slots:
        void slotFunc (int i = 0) {}
    };
    QObject :: connect (A, SIGNAL (sigFunc (void)), B, SLOT (slotFunc (void) )); // ok
3) The parameters of the signal function can be more than the slot function parameters, the excess parameters will be ignored and the    opposite will not be possible
    QObject :: connect (A, SIGNAL (sigFunc (int)), B, SLOT (slotFunc (void ))); // ok
4) A signal can be connected to multiple slot functions (one-to-many)
    QObject :: connect (A, SIGNAL (sigFunc (int)), B1, SLOT (slotFunc1 (int))); / / ok
    QObject :: connect (A, SIGNAL (sigFunc (int)), B2, SLOT (slotFunc2 (int))); // OK
    Note: If the A object sends preferences, the slot functions of B1 and B2 will be executed
5) Multiple signals can be connected to the same slot function at the same time (many to one)
    QObject :: connect (A1, SIGNAL (sigFunc1 (int)), B, SLOT (slotFunc (int))); // ok
    QObject :: connect (A2, SIGNAL (sigFunc2 (int)), B, SLOT (slotFunc (int))); // ok
    Note: No matter whether A1 or A2 sends a signal, the slot function of B will be executed
6) Two signals The function can be directly connected (signal in series) // Understand
    QObject :: connect (A1, SIGNAL (sigFunc1 (int)), A2, SIGNAL (sigFunc2 (int))); // OK
    Note: When A1 sends a signal, it is connected The signal of the A2 object will also be sent.

 

Lambda expressions are also used many times when applying slot functions;

Lambda expression: used to define and create anonymous function objects.

Lambda's syntax is as follows:

              [Function object parameter] ( Operator overload function parameter) mutable or exception declaration-> return value type {function body}

              [] Is a lambda expression, choose the way to pass

             [](){Btn->settext("aaaa");};

            1, value transfer

             [=](){Btn->settext("aaaa");};

            2, address transfer

             [&](){Btn->settext("aaaa");};

For specific usage, refer to Baidu.

Published 9 original articles · praised 6 · visits 1996

Guess you like

Origin blog.csdn.net/GG802312/article/details/105416807