Qt function connectslotsbyname (), that is, description of "qmetaobject: connectslotsbyname (qobject * o )"

Source: Internet
Author: User

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.

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.