技术博客1

xiaoxiao2021-02-28  51

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()的宏定义:

#ifndef QT_NO_DEBUG   # define QLOCATION "\0"__FILE__":"QTOSTRING(__LINE__)   # define METHOD(a)   qFlagLocation("0"#a QLOCATION)   # define SLOT(a)     qFlagLocation("1"#a QLOCATION)   # define SIGNAL(a)   qFlagLocation("2"#a QLOCATION)   #else   # define METHOD(a)   "0"#a   # define SLOT(a)     "1"#a   # define SIGNAL(a)   "2"#a   #endif  

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

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

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

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

转载请注明原文地址: https://www.6miu.com/read-81593.html

最新回复(0)