Qt connect函数的深入研究

来源:互联网 发布:农产品追溯软件 编辑:程序博客网 时间:2024/05/18 07:25

connect,是QT中的连接函数,将信号发送者sender对象中的信号signal与接受者receiver中的member槽函数联系起来。 

从Qobject(QObject.h)源码中可以看到QObject::connect的定义:

  1. static bool connect(const QObject *sender, const char *signal,  
  2.                     const QObject *receiver, const char *member, Qt::ConnectionType =  
  3.     #ifdef qdoc  
  4.                         Qt::AutoConnection  
  5.     #else  
  6.         #ifdef QT3_SUPPORT  
  7.                             Qt::AutoCompatConnection  
  8.     #else  
  9.                                 Qt::AutoConnection  
  10.         #endif  
  11.     #endif  
  12.     );  
  13. inline bool connect(const QObject *sender, const char *signal,  
  14.                     const char *member, Qt::ConnectionType type =  
  15.     #ifdef qdoc  
  16.                      Qt::AutoConnection  
  17.     #else  
  18.         #ifdef QT3_SUPPORT  
  19.                                 Qt::AutoCompatConnection  
  20.         #else  
  21.                                 Qt::AutoConnection  
  22.         #endif  
  23.     #endif  
  24.     ) const;  


在使用connect函数的时候一般是这样调用的:

connect(sender,SIGNAL(signal()),receiver,SLOT(slot()));  

两个宏:SIGNAL() 和SLOT();通过connect声明可以知道这两个宏最后倒是得到一个const char*类型。
在qobjectdefs.h中可以看到SIGNAL() 和SLOT()的宏定义:

  1. #ifndef QT_NO_DEBUG  
  2. # define QLOCATION "\0"__FILE__":"QTOSTRING(__LINE__)  
  3. # define METHOD(a)   qFlagLocation("0"#a QLOCATION)  
  4. # define SLOT(a)     qFlagLocation("1"#a QLOCATION)  
  5. # define SIGNAL(a)   qFlagLocation("2"#a QLOCATION)  
  6. #else  
  7. # define METHOD(a)   "0"#a  
  8. # define SLOT(a)     "1"#a  
  9. # define SIGNAL(a)   "2"#a  
  10. #endif 

所以这两个宏的作用就是把函数名转换为字符串并且在前面加上标识符。

比如:SIGNAL(read())展开后就是"2read()";同理SLOT(read())展开后就是"1read()"

在QObject.cpp文件中可以找到connect的实现代码:

  1. bool QObject::connect(const QObject *sender, const char *signal,  
  2.                       const QObject *receiver, const char *method,  
  3.                       Qt::ConnectionType type)  
  4. {  
  5.     {  
  6.         const void *cbdata[] = { sender, signal, receiver, method, &type };  
  7.         if (QInternal::activateCallbacks(QInternal::ConnectCallback, (void **) cbdata))  
  8.             return true;  
  9.     }  
  10.   
  11.     if (sender == 0 || receiver == 0 || signal == 0 || method == 0) {  
  12.         qWarning("QObject::connect: Cannot connect %s::%s to %s::%s",  
  13.                  sender ? sender->metaObject()->className() : "(null)",  
  14.                  (signal && *signal) ? signal+1 : "(null)",  
  15.                  receiver ? receiver->metaObject()->className() : "(null)",  
  16.                  (method && *method) ? method+1 : "(null)");  
  17.         return false;  
  18.     }  
  19.     QByteArray tmp_signal_name;  
  20.   
  21.     if (!check_signal_macro(sender, signal, "connect""bind"))  
  22.         return false;  
  23.     const QMetaObject *smeta = sender->metaObject();  
  24.     const char *signal_arg = signal;  
  25.     ++signal; //skip code  
  26.     int signal_index = smeta->indexOfSignal(signal);  
  27.     if (signal_index < 0) {  
  28.         // check for normalized signatures  
  29.         tmp_signal_name = QMetaObject::normalizedSignature(signal - 1);  
  30.         signal = tmp_signal_name.constData() + 1;  
  31.   
  32.         signal_index = smeta->indexOfSignal(signal);  
  33.         if (signal_index < 0) {  
  34.             err_method_notfound(sender, signal_arg, "connect");  
  35.             err_info_about_objects("connect", sender, receiver);  
  36.             return false;  
  37.         }  
  38.     }  
  39.   
  40.     QByteArray tmp_method_name;  
  41.     int membcode = extract_code(method);  
  42.   
  43.     if (!check_method_code(membcode, receiver, method, "connect"))  
  44.         return false;  
  45.     const char *method_arg = method;  
  46.     ++method; // skip code  
  47.   
  48.     const QMetaObject *rmeta = receiver->metaObject();  
  49.     int method_index = -1;  
  50.     switch (membcode) {  
  51.     case QSLOT_CODE:  
  52.         method_index = rmeta->indexOfSlot(method);  
  53.         break;  
  54.     case QSIGNAL_CODE:  
  55.         method_index = rmeta->indexOfSignal(method);  
  56.         break;  
  57.     }  
  58.     if (method_index < 0) {  
  59.         // check for normalized methods  
  60.         tmp_method_name = QMetaObject::normalizedSignature(method);  
  61.         method = tmp_method_name.constData();  
  62.         switch (membcode) {  
  63.         case QSLOT_CODE:  
  64.             method_index = rmeta->indexOfSlot(method);  
  65.             break;  
  66.         case QSIGNAL_CODE:  
  67.             method_index = rmeta->indexOfSignal(method);  
  68.             break;  
  69.         }  
  70.     }  
  71.   
  72.     if (method_index < 0) {  
  73.         err_method_notfound(receiver, method_arg, "connect");  
  74.         err_info_about_objects("connect", sender, receiver);  
  75.         return false;  
  76.     }  
  77.     if (!QMetaObject::checkConnectArgs(signal, method)) {  
  78.         qWarning("QObject::connect: Incompatible sender/receiver arguments"  
  79.                  "\n        %s::%s --> %s::%s",  
  80.                  sender->metaObject()->className(), signal,  
  81.                  receiver->metaObject()->className(), method);  
  82.         return false;  
  83.     }  
  84.   
  85.     int *types = 0;  
  86.     if ((type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection)  
  87.             && !(types = queuedConnectionTypes(smeta->method(signal_index).parameterTypes())))  
  88.         return false;  
  89.   
  90.     QMetaObject::connect(sender, signal_index, receiver, method_index, type, types);  
  91.     const_cast<QObject*>(sender)->connectNotify(signal - 1);  
  92.     return true;  
  93. }  

判断连接是否已经建立:

  1. const void *cbdata[] = { sender, signal, receiver, method, &type };  
  2. if (QInternal::activateCallbacks(QInternal::ConnectCallback, (void **) cbdata))  
  3.       return true;
QInternal::ConnectCallback在qglobal.cpp中实现:

  1. bool QInternal::activateCallbacks(Callback cb, void **parameters)  
  2. {  
  3.     Q_ASSERT_X(cb >= 0, "QInternal::activateCallback()""Callback id must be a valid id");  
  4.   
  5.     QInternal_CallBackTable *cbt = global_callback_table();  
  6.     if (cbt && cb < cbt->callbacks.size()) {  
  7.         QList<qInternalCallback> callbacks = cbt->callbacks[cb];  
  8.         bool ret = false;  
  9.         for (int i=0; i<callbacks.size(); ++i)  
  10.             ret |= (callbacks.at(i))(parameters);  
  11.         return ret;  
  12.     }  
  13.     return false;  
  14. }  
  1. const QMetaObject *smeta = sender->metaObject();  
  2. const char *signal_arg = signal;  
  3. ++signal; //skip code  
  4. int signal_index = smeta->indexOfSignal(signal);  
  5. if (signal_index < 0) {  
  6.     // check for normalized signatures  
  7.     tmp_signal_name = QMetaObject::normalizedSignature(signal - 1);  
  8.     signal = tmp_signal_name.constData() + 1;  
  9.   
  10.     signal_index = smeta->indexOfSignal(signal);  
  11.     if (signal_index < 0) {  
  12.         err_method_notfound(sender, signal_arg, "connect");  
  13.         err_info_about_objects("connect", sender, receiver);  
  14.         return false;  
  15.     }  
  16. }  

qt_meta_stringdata_MainWindow(具体名字和类名有关)就是staticconstchar[]类型。它记录了全部的signals和slots等的函数名、返回值和参数表的信息。

qt_meta_data_MainWindow(具体名字和类名有关)是staticconstuint[]类型。它记录了每一个函数的函数名、返回值和参数表在qt_meta_stringdata_MainWindow中的索引。同时它还记录了每一个函数的类型具体在qmetaobject.cpp文件中定义。

  1. enum MethodFlags  {  
  2.     AccessPrivate = 0x00,  
  3.     AccessProtected = 0x01,  
  4.     AccessPublic = 0x02,  
  5.     AccessMask = 0x03, //mask  
  6.   
  7.     MethodMethod = 0x00,  
  8.     MethodSignal = 0x04,  
  9.     MethodSlot = 0x08,  
  10.     MethodConstructor = 0x0c,  
  11.     MethodTypeMask = 0x0c,  
  12.   
  13.     MethodCompatibility = 0x10,  
  14.     MethodCloned = 0x20,  
  15.     MethodScriptable = 0x40  
  16. };  
indexOfSignal(signal);的实现在qmetaobject.cpp中。其主要作用是利用qt_meta_stringdata_MainWindow 和qt_meta_data_MainWindow查找已经定义了的signal并返回索引。

原创粉丝点击