技术博客1

来源:互联网 发布:黑客入侵网站盗取数据 编辑:程序博客网 时间:2024/06/04 00:59

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

QObject::connect的定义是这样的:

    static bool connect(const QObject *sender, const char *signal,
                        const QObject *receiver, const char *member, Qt::ConnectionType =
        #ifdef qdoc
                         Qt::AutoConnection
        #else
         #ifdef QT3_SUPPORT
                          Qt::AutoCompatConnection
        #else
                              Qt::AutoConnection
         #endif
        #endif
        );
    inline bool connect(const QObject *sender, const char *signal,
                        const char *member, Qt::ConnectionType type =
        #ifdef qdoc
                         Qt::AutoConnection
        #else
         #ifdef QT3_SUPPORT
                              Qt::AutoCompatConnection
         #else
                              Qt::AutoConnection
         #endif
        #endif
        ) const;

其中第二个connect的实现其实只有一句话:

return connect(asender, asignal, this, amember, atype); }  

所以对于connect函数的学习其实就是研究第一个connect函数。

在使用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()"。

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

实际上就是connect(sender,“2signal()”,receiver,“1slot())”;  


原创粉丝点击