Qt函数connectSlotsByName(),即"QMetaObject::connectSlotsByName(QObject *o)"的说明

 connectSlotsByName()函数源码如下:

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);
         }
     }
} 

按下F1,出现如下说明:

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().

connectSlotsByName()函数说明:
connectSlotsByName 是一个QMetaObject类里的static函数,其作用是用来将QObject *o里的子QObject的某些信号按照其objectName连接到o的槽上。

注意:
1. 如果QObject *o中有多个同名的子object,connectSlotsByName只能给其中一个建立缺省的信号和槽的连接。
2. connectSlotsByName更适合的任务是与Qt desinger配合完成缺省的信号和槽的连接。

你可能感兴趣的:(object,search,Parameters,qt,button,Signal)