In the UI design interface, when designing signals and slots, the information will be present in the UI file, and the connection code will be added to the Chengcheng header file **.h at compile time.
There are two ways to add signals and slots to the UI interface
The first kind: In the interface selection Operation Object-right-"go to Groove-" SELECT Signal-"Qtcreator automatic generation Slot Method-" write the slot method. This method does not save the response signal with slot information in the UI file because it generates a fixed slot format, such as on_countbtn_clicked. Add Qmetaobject::connectslotsbyname (MainWindow) at compile time, and the method can automatically map to the corresponding connect by method name.
The second type: below the editing interface
This method generates the appropriate connect information in the UI file, and at compile time, it is added in the **.h header file.
Connect (lineedit,signal (textChanged (QString)), This,slot (Showarea ())) method.
The following is the first method of principle explanation
When you view some of the sample projects for QT5, you open their UI files with the designer, and you cannot find a connection to the signal and slots in the file. However, the final program, the slot can accurately respond to the signal. Opens a C + + file that is automatically generated through the UI file, where the Connect statement is not found.
After a statement-by-sentence analysis. Finally, the reason for the connection is that the last sentence of the Setui function
Qmetaobject::connectslotsbyname (MainWindow);
Locate the static function
void Qmetaobject::connectslotsbyname (Qobject *o) {if (!o) return;
Const Qmetaobject *MO = O->metaobject ();
Q_assert (MO);
Const Qobjectlist list = Qfindchildren (O, QString ());
for (int i = 0; i < Mo->methodcount (); ++i) {const char *slot = Mo->method (i). Signature ();
Q_assert (slot);
The following line is used to determine if the first three bits of the slot are on_, and if not, skip this method.
if (slot[0]! = ' O ' | | slot[1]! = ' n ' | | slot[2]! = ' _ ') continue;
BOOL Foundit = false;
Traverse child objects.
for (int j = 0; J < List.count (); ++j) {const Qobject *co = list.at (j);
Gets the child object name.
Qbytearray objname = Co->objectname (). Toascii ();
int len = Objname.length (); After the slot is required to skip the first 3 bits (On_), the next substring and sub-object name are the same, and then the substring is another _//If this requirement is not met, continue if (!len | | qstrncmp (slot + 3, ObjName.dat A (), Len) | |
SLOT[LEN+3]! = ' _ ') continue;
Const Qmetaobject *smo = Co->metaobject ();
int sigindex = Smo->indexofmethod (slot + len + 4); if (Sigindex < 0) {//Search for compatible Signals int slotlen = Qstrlen (slot + len + 4)-1; Search for the signal that this sub-object can raise for (int k = 0; k < Co->metaobject ()->methodcount (); ++k) {//method type if required if (smo->m
Ethod (k). Methodtype ()! = qmetamethod::signal) Continue;
If the last substring of the slot is the same as the signal name if (!qstrncmp (Smo->method (k). Signature (), slot + len + 4, Slotlen)) {sigindex = k;
Break
}}} if (Sigindex < 0) continue;
Connection operation if (Qmetaobject::connect (Co, sigindex, O, i)) {Foundit = true;
Break }}//Connection succeeded if (Foundit) {//We found our slots, now skip all overloads while (Mo->method (i + 1). attrib
Utes () & qmetamethod::cloned) ++i; }//Connection failed else if (! ( Mo->method (i). attributes () & qmetamethod::cloned)) {qwarning ("Qmetaobject::connectslotsbyname:no matching
Signal for%s ", slot); } } }
This concludes that the function always has the last sentence of the Setui function in the automatically generated file.
The function is to find the Setui unique pointer parameter MainWindow the member function of the object,
The name of the member function is the connection operation if the following conditions are met.
Function name rule: On_ Sub-object name _ signal name
function signature (that is, the return value and parameters must meet the slot requirements)
So, we can do this: after adding a button or a menu item or a button in the QT Designer, you do not have to do the connection operation manually in the designer.
We just add the qualifying member function to the main window class.
Function name rule: On_ Sub-object name _ signal name
function signature (that is, the return value and parameters must meet the slot requirements)
For example:
Add a menu item to the designer with the corresponding action of actionnew
Then add the following function to the main window class
Public Slots:
void On_actionnew_triggered ();
When you switch this menu, the above member functions are automatically executed.