Signal & Slot in Qt

Try your best to provide an mechanism to implement what you want.

1. All is generated by QT Framework before compiling.

 1 QObject.connect(sender,SIGNAL(signal()), reciver,SLOT(slot())); 

==>

QObject.connect(sender, “2signal( )”,reciver,”1slot()”);

 

 1 class QExample : public QObject
 2 {
 3 
 4        Q_OBJECT
 5 
 6 public:
 7        QTestA (QObject *parent );
 8 
 9        ~QTestA ();
10 signals:
11        void SignalA1 ();
12        void SignalA2 (int i );
13 
14 public slots:
15        void SlotA1 ();
16        void SlotA2 (char *szBuf ,int nSize );
17 private:
18 
19 public:
20 
21 int qt_metacall (QMetaObject ::Call _c , int _id , void **_a )
22 {
23     _id = QObject ::qt_metacall (_c , _id , _a );
24     if (_id < 0)
25         return _id ;
26     if (_c == QMetaObject ::InvokeMetaMethod ) 
27    {
28         switch (_id ) {
29         case 0: SignalA1 (); break ;
30         case 1: SignalA2 ((*reinterpret_cast < int (*)>(_a [1]))); break ;
31         case 2: SlotA1 (); break ;
32         case 3: SlotA2 ((*reinterpret_cast < char *(*)>(_a [1])),(*reinterpret_cast < int (*)>(_a [2]))); break ;
33         default : ;
34          }
35         _id -= 4;
36     }
37     return _id ;
38 }
39       
40 
41 }; 

2. Add additional meta data and function in class

1  QMetaObject 
2 {
3     const QMetaObject *superdata ;
4     const char *stringdata ; //save class name, signal name, parameter name,slot name, all ended by '/0'
5     const uint *data ; //int array, store information of QMetaObjectPrivate
6 }
 1 struct QMetaObjectPrivate
 2 
 3 {
 4 
 5     int revision ;
 6     int className ;
 7     int classInfoCount , classInfoData ;
 8     int methodCount , methodData ;
 9     int propertyCount , propertyData ;
10     int enumeratorCount , enumeratorData ;
11     int constructorCount , constructorData ;
12 }; 

 

 

3. Define your own macro, translate user input string into the real code

All below is added in order to find the right ID for cooresponding method of signal and slot

Example:

 

 1 static const uint qt_meta_data_Q QExampleData[] = {
 2        2,       // revision
 3        0,       // classname
 4        0,    0, // classinfo  
 5        4,   12, // methods and its data
 6        0,    0, // properties
 7        0,    0, // enums/sets
 8        0,     0, // constructors
 9        // 以上部分 是QMetaObjectPrivate 结构信息
10        // signals: signature, parameters, type, tag, flags
11        8,    7,    7,    7, 0x05,
12       21,   19,    7,    7, 0x05,
13       // slots: signature, parameters, type, tag, flags
14       35,    7,    7,    7, 0x0A,
15       56,   44,    7,    7, 0x0A,
16       0        // end
17 }; 

 

 

 

 

// slots: signature, parameters, type, tag, flags
1
static const char qt_meta_stringdata_Q QExampleMetaData [] = { 2 "QTestA/0/0SignalA1()/0i/0SignalA2(int)/0" 3 "SlotA1()/0szBuf,nSize/0SlotA2(char*,int)/0" 4 };

 

 

 

 

4.And connection object to list

 

 1 QMetaObject::connect(sender, signal_index,receiver, method_index,type, types);
 2 QObject *s =const_cast<QObject*>(sender);
 3 QObject*r = const_cast<QObject *>(receiver);
 4 
 5 QOrderedMutexLockerlocker(&s->d_func()->threadData->mutex,                       &r->d_func()->threadData->mutex);
 6 
 7 QObjectPrivate::Connection c;
 8 
 9 c.receiver = r;
10 c.method = method_index;
11 c.connectionType = type;
12 c.argumentTypes= types;
13 
14 s->d_func()->addConnection(signal_index, &c);
15 r->d_func()->refSender(s, signal_index);

 

 

 

QObject::connect(const QObject *sender, 
                 const char *signal,  
                 const QObject *receiver, 
                 const char *method,  
                 Qt::ConnectionType type)  
{  
    {  
        const void *cbdata[] = { sender, signal, receiver, method, &type };  
        if (QInternal::activateCallbacks(QInternal::ConnectCallback, (void **) cbdata))  
            return true;  
    }  
  
    if (sender == 0 || receiver == 0 || signal == 0 || method == 0) {  
        qWarning("QObject::connect: Cannot connect %s::%s to %s::%s",  
                 sender ? sender->metaObject()->className() : "(null)",  
                 (signal && *signal) ? signal+1 : "(null)",  
                 receiver ? receiver->metaObject()->className() : "(null)",  
                 (method && *method) ? method+1 : "(null)");  
        return false;  
    }
    
    QByteArray tmp_signal_name;  
    
    if (!check_signal_macro(sender, signal, "connect", "bind"))  
        return false;  
        
    const QMetaObject *smeta = sender->metaObject();  
    const char *signal_arg = signal;  
    ++signal; 
    
    //skip code 
    int signal_index = smeta->indexOfSignal(signal);  
    if (signal_index < 0) {  
        // check for normalized signatures  
        tmp_signal_name = QMetaObject::normalizedSignature(signal - 1);  
        signal = tmp_signal_name.constData() + 1;  
  
        signal_index = smeta->indexOfSignal(signal);  
        if (signal_index < 0) {  
            err_method_notfound(sender, signal_arg, "connect");  
            err_info_about_objects("connect", sender, receiver);  
            return false;  
        }  
    }  
  
    QByteArray tmp_method_name;  
    int membcode = extract_code(method);  
  
    if (!check_method_code(membcode, receiver, method, "connect"))  
        return false;  
    const char *method_arg = method;  
    ++method; // skip code  
  
    const QMetaObject *rmeta = receiver->metaObject();  
    int method_index = -1;  
    switch (membcode) {  
    case QSLOT_CODE:  
        method_index = rmeta->indexOfSlot(method);  
        break;  
    case QSIGNAL_CODE:  
        method_index = rmeta->indexOfSignal(method);  
        break;  
    }  
    if (method_index < 0) {  
        // check for normalized methods  
        tmp_method_name = QMetaObject::normalizedSignature(method);  
        method = tmp_method_name.constData();  
        switch (membcode) {  
        case QSLOT_CODE:  
            method_index = rmeta->indexOfSlot(method);  
            break;  
        case QSIGNAL_CODE:  
            method_index = rmeta->indexOfSignal(method);  
            break;  
        }  
    }  
  
    if (method_index < 0) {  
        err_method_notfound(receiver, method_arg, "connect");  
        err_info_about_objects("connect", sender, receiver);  
        return false;  
    }  
    if (!QMetaObject::checkConnectArgs(signal, method)) {  
        qWarning("QObject::connect: Incompatible sender/receiver arguments"  
                 "\n        %s::%s --> %s::%s",  
                 sender->metaObject()->className(), signal,  
                 receiver->metaObject()->className(), method);  
        return false;  
    }  
  
    int *types = 0;  
    if ((type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection)  
            && !(types = queuedConnectionTypes(smeta->method(signal_index).parameterTypes())))  
        return false;  
  
    QMetaObject::connect(sender, signal_index, receiver, method_index, type, types);  
    const_cast<QObject*>(sender)->connectNotify(signal - 1);  
    return true;  
}
View Code

 

 

 

posted @ 2014-12-10 19:36  iDragon  阅读(349)  评论(0编辑  收藏  举报