[Qt] Signals and Slots in depth

Source: Internet
Author: User
Tags qt designer

Signals and slots are an important part of QT programming. This mechanism can associate the actions of objects without mutual understanding. In the previous examples, we have connected the signal and slot, declared the signal and slot of the control, implemented the slot function, and sent our own signal. Now let's have a better understanding of this mechanism.

The slot is similar to a common C ++ member function. They can be virtual functions, overloads, public functions, proteve ve, or private functions ), they can be called directly like any c ++ member function, and any type of parameters can be passed. The difference is that a slot function can be connected to a signal. As long as the signal is sent, this slot function will be automatically called.

The connect function syntax is as follows:
Connect (sender, signal (signal), explorer, slot (slot ));
Sender and consumer er are qobject object pointers, while signal and slot are function prototypes without parameters. The functions of signale () and slot () macros are to convert their parameters into strings.

In the current example, we have connected different signals and slots. The following rules must be taken into account in actual use:
1. One signal can be connected to multiple slots:
Connect (slider, signal (valuechanged (INT), spinbox, slot (setvalue (INT )));
Connect (slider, signal (valuechanged (INT), this, slot (updatestatusbarindicator (INT )));
When the signal is sent, the slot function is called, but the call sequence is random and uncertain.

2. Multiple signals can be connected to the same slot
Connect (LCD, signal (overflow (), this, slot (handlematherror ()));
Connect (calculator, signal (divisionbyzero (), this, slot (handlematherror ()));
The slot function is executed when any signal is sent.

3. One signal can be connected to another signal
Connect (lineedit, signal (textchanged (const qstring &)),
This, signal (UpdateRecord (const qstring &)));
After the first signal is sent, the second signal is also sent. In addition, the connection between signals is the same as that between signals and slots.
 
4. The connection can be deleted.
Disconnect (LCD, signal (overflow (), this, slot (handlematherror ()));
This function is rarely used, because after an object is deleted, QT automatically deletes all connections of this object.

The signal and slot functions must have the same parameter type and sequence so that the signal and slot functions can be connected successfully:
Connect (FTP, signal (rawcommandreply (INT, const qstring &), this, slot (processreply (INT, const qstring &)));
If the number of parameters in the signal is greater than that of the slot function, extra parameters are ignored:
Connect (FTP, signal (rawcommandreply (INT, const qstring &), this, slot (checkerrorcode (INT )));
If the parameter speed type does not match, or the signal and slot do not exist, Qt will give a warning during running when the application is in debug state. If the signal and the slot connection contain the parameter name, Qt will also give a warning.
In the previous examples, the signals and slots of controls are used. However, the signal and slot mechanisms are implemented in QObject and can be implemented in any subclass inherited from QObject.
Class Employee: public QObject
{
Q_OBJECT
Public:
Employee () {mySalary = 0 ;}
Int salary () const {return mySalary ;}
Public slots:
Void setSalary (int newSalary );
Signals:
Void salaryChanged (int newSalary );
Private:
Int mySalary;
};
Void Employee: setSalary (int newSalary)
{
If (newSalary! = MySalary ){
MySalary = newSalary;
Emit salaryChanged (mySalary );
}
}
Note: Only newSalary! = MySalary.

××××××××××××××××××××××××××××××××××××××××××× ××××××××××××××××××××××××××××××××××××××××××× ×××
Qt Meta-Object System
One major success of Qt is the expansion of C ++, that is, connecting independent software modules, while modules do not need to know any details about the connected modules.
This mechanism is the Meta-Object System, which provides two key purposes: Signal and slot and introspection (introspection ). The introspection function allows programmers to obtain the "meta-information" of its subclass of QObject when the program is running, which is necessary to implement signals and slots, including the list of all signals and slots, and the class name. This mechanism also supports attributes (used in Qt Designer) and Text Translation (internationalization. These constitute the basis of QSA (Qt Script for Application.

Standard C ++ does not provide the dynamic meta-information required by the Qt meta-object System. To solve this problem, Qt provides an independent tool moc, which uses the C ++ function to parse the definition of the Q_OBJECT macro class and obtain information. Moc is implemented in pure c ++. Therefore, the object meta system can be used in any C ++ compiler.

This mechanism works as follows:
The Q_OBJECT macro declares the internal functions that must be implemented in some QObject sub-classes: metaObject (), TR (), qt_metacall () and so on.
The moc tool of Qt implements the Q_OBJECT macro acoustic function and all signals.
QObject member functions such as connect () and disconnect () Use these introspection functions to connect signals and slots.
These are automatically processed through qmake, moc, and QObject. Programmers generally do not need to consider them. If you are curious about this, you can view the QMetaObject Class documentation and the c ++ source file generated by moc.

 

Address: http://kruck.banzhu.net/article/kruck-3-177347.html

 

 

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.