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配合完成缺省的信号和槽的连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值