The source code of the connectslotsbyname () function is as follows:
void QMetaObject::connectSlotsByName(QObject *o){ if (!o) return; const QMetaObject *mo = o->metaObject(); Q_ASSERT(mo); const QObjectList list = qFindChildren<QObject *>(o, QString()); for (int i = 0; i < mo->methodCount(); ++i) { const char *slot = mo->method(i).signature(); Q_ASSERT(slot); if (slot[0] != 'o' || slot[1] != 'n' || slot[2] != '_') continue; bool foundIt = false; for(int j = 0; j < list.count(); ++j) { const QObject *co = list.at(j); QByteArray objName = co->objectName().toAscii(); int len = objName.length(); if (!len || qstrncmp(slot + 3, objName.data(), 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; for (int k = 0; k < co->metaObject()->methodCount(); ++k) { if (smo->method(k).methodType() != QMetaMethod::Signal) continue; if (!qstrncmp(smo->method(k).signature(), slot + len + 4, slotlen)) { sigIndex = k; break; } } } if (sigIndex < 0) continue; if (QMetaObject::connect(co, sigIndex, o, i)) { foundIt = true; break; } } if (foundIt) { // we found our slot, now skip all overloads while (mo->method(i + 1).attributes() & QMetaMethod::Cloned) ++i; } else if (!(mo->method(i).attributes() & QMetaMethod::Cloned)) { qWarning("QMetaObject::connectSlotsByName: No matching signal for %s", slot); } }}
Press F1 to see the following description:
void QMetaObject::connectSlotsByName ( QObject * object ) [static]Searches recursively for all child objects of the given object, and connects matching signals from them to slots of object that follow the following form: void on_<object name>_<signal name>(<signal parameters>);Let's assume our object has a child object of type QPushButton with the object name button1. The slot to catch the button's clicked() signal would be: void on_button1_clicked();See also QObject::setObjectName().
Description of the connectslotsbyname () function:
Connectslotsbyname is a static function in the qmetaobject class. It is used to connect some signals of sub-qobject in qobject * O to the O slot according to its objectname.
Note: 1. If qobject * o contains multiple sub-objects with the same name, connectslotsbyname can only establish a default signal and slot connection for one of them. 2. connectslotsbyname is more suitable for tasks that work with QT desinger to connect the default signal and slot.