Logo Search packages:      
Sourcecode: qt4-x11 version File versions

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_<widget name>_<signal name>(<signal parameters>);

Let's assume our object has a child object of type QPushButton with the {QObject::objectName}{object name} {button1}. The slot to catch the button's {clicked()} signal would be:

    void on_button1_clicked();

See also:
QObject::setObjectName()

Definition at line 2898 of file qobject.cpp.

References QList< T >::at(), QMetaMethod::attributes(), connect(), QList< T >::count(), QByteArray::data(), indexOfMethod(), QByteArray::length(), method(), methodCount(), QMetaMethod::methodType(), QObject::objectName, QMetaMethod::signature(), and QString::toAscii().

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


Generated by  Doxygen 1.6.0   Back to index