Signal and slot)
- Similar to messages and message responses in windows
- All implemented through C ++ class member functions
- Signals and slots are connected to each other.
- Classes containing signals or slots must inherit from QObject
Signal (signal) and slot -- Declaration
class Employee : public QObject{Q_OBJECTpublic:Employee();int salary() const;public slots:void setSalary(int newSalary);signals:void salaryChanged(int newSalary);private:int mySalary;};emit salaryChanged(50);Signal (signal) and slot-connection
Connect (sender, SIGNAL (signal), explorer, SLOT (slot ));
1. one SIGNAL can be connected to multiple slots. The call sequence of these slots is random connect (slider, SIGNAL (valueChanged (int), spinBox, SLOT (setValue (int ))); connect (slider, SIGNAL (valueChanged (int), this, SLOT (updateStatusBarIndicator (int); 2. one SLOT can connect multiple signals, and each SIGNAL can trigger the connect (LCD, SIGNAL (overflow (), this, SLOT (handleMathError (); connect (calculator, SIGNAL (divisionByZero (), this, SLOT (handleMathError (); 3. signals can be connected to each other, and the connected signals will trigger connect (lineEdit, SIGNAL (textChanged (const QString &), this, SIGNAL (updateRecord (const QString &))); 4. the slot and slot cannot be connected to each other.
The connection between the signal and the signal, the signal and the slot is at run time, and the connection can be canceled at run time. Qt will automatically cancel the connection when appropriate, therefore, it is generally not necessary to manually cancel connect (LCD, SIGNAL (overflow (), this, SLOT (handleMathError (); disconnect (LCD, SIGNAL (overflow ()), this, SLOT (handleMathError ()));
The signal and the slot must have the same parameter list. If there are more signal parameters than the slot, extra parameters will be ignored. If the parameter list does not match, qt will generate runtime error messages connect (ftp, SIGNAL (rawCommandReply (int, const QString &), this, SLOT (processReply (int, const QString &))); connect (ftp, SIGNAL (rawCommandReply (int, const QString &), this, SLOT (checkErrorCode (int )));
Reference: Introduction to QT signal and Slot Mechanism