[QT] signal and Slot Mechanism

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.