首页 > 技术文章 > Qt之connect

sherlock-lin 2019-10-20 20:18 原文

Qt4之connect

基本用法

connect(ui->toolButton, SIGNAL(clicked()), this, SLOT(OnClickButton()));
disconnect(ui->toolButton, SIGNAL(clicked()), this, SLOT(OnClickButton()));

返回bool以表示信号槽连接/断开是否成功

Q_CORE_EXPORT const char *qFlagLocation(const char *method);

#define SLOT(a)     qFlagLocation("1"#a QLOCATION)
#define SIGNAL(a)   qFlagLocation("2"#a QLOCATION)

SIGNALSLOT实际上是两个宏定义,将函数名及其参数列表转换成一个字符串,随后moc会扫面全部文件,将所有的信号和槽提取出来做成一个映射表。

设置回调函数

利用SIGNALSLOT两个宏定义转换字符串的功能,可以利用之设置回调函数

void SetCallback(const char* callback){
    connect(this, SIGNAL(TestSignal()), this, callback);
}
SetCallback(SLOT(TargetFunction));	//调用

Qt5之connect

  • Qt5兼容旧版的信号槽连接机制;
  • 新增新语法连接信号槽,使用函数指针;

示例:

QMetaObject::Connection cobject = connect(ui->toolButton, &QToolButton::clicked, this, &Widget::OnClickButton);

disconnect(connect_object);
connect(ui->toolButton, &QToolButton::clicked, TestFunction)
int temp = 123;
connect(ui->toolButton, &QToolButton::clicked, [temp](){ 
    qDebug()<<"Test Function"<<temp; 
});	//lambda

优点:

  • 编译检查;
  • 参数自动隐式转换;
  • 不需要指定public/private slots
  • 不需要moc,任何函数都可以作为槽函数;

缺点:

  • 信号不能重载,若有信号重载,则编译错误(信号槽应该避免使用重载);
  • 不支持槽函数的默认参数;

推荐阅读