Qt信号槽的两种写法

Qt信号槽connect是什么?

  • connect()函数的形式
connect(sender, signal, receiver, slot,type);
  • 参数示意
  1. sender:发出信号的对象
  2. signal:发送对象发出的信号
  3. receiver:要接收信号的对象
  4. slot:接收对象收到信号后调用的函数
  5. type:连接方式,默认自动auto,可设置成同步Driect或异步Quence等方式,可参考品前文

传统Qt4写法(极度不推荐)

 connect(btn_close, SIGNAL(clicked()), this, SLOT(DoCloseSlot()));

不推荐的原因:

  • SIGNAL和SLOT两个宏是将函数名称,转换成了字符串,connect连接不成功,不会在编译时后报错,排查问题困难
  • 槽函数必须定义在slot宏下
public:
   void DoCloseSlot();//错误,连接失败,没有注册元对象
public slot:
    void DoCloseSlot();//成功
  • 槽函数在slot下,但参数不匹配,仍然连接失败,无法编译时报错
//xxx.h
signals:
    void testMocSignal(int input,int in2);
protected slots:
   void DoCloseSlot(QString a,QString b);
//xxx.cpp
connect(this, SIGNAL(testMocSignal(int,int)), this, SLOT(DoCloseSlot(QString,QString)));
//编译时不报错,运行时输出:

/*
QObject::connect: Incompatible sender/receiver arguments
        TestMoc::testMocSignal(int,int) --> TestMoc::DoCloseSlot(QString,QString)
*/

但下面这种情况可以响应槽函数

//xxx.h
signals:
    void testMocSignal(int in1,int in2);
    void testMocSignal2();
    void testMocSignal3(int in1);
protected slots:
   void DoCloseSlot();
   void DoCloseSlot2(int in1,int in2);
   void DoCloseSlot3(int in1,int in2=999);
    void DoCloseSlot4(int in1=999,int in2=999);
//xxx.cpp 

//可以正常响应,连接成功
connect(this, SIGNAL(testMocSignal(int,int)), this, SLOT(DoCloseSlot()));
//无法响应,连接失败
connect(this, SIGNAL(testMocSignal2()), this, SLOT(DoCloseSlot2(int,int)));
//可以正常响应,连接成功,槽函数缺省值有默认参数
connect(this, SIGNAL(testMocSignal3(int)), this, SLOT(DoCloseSlot3(int)));
//无法响应,连接失败,无法找到匹配的testMocSignal3信号
connect(this, SIGNAL(testMocSignal3()), this, SLOT(DoCloseSlot4()));
/*
DoCloseSlot 可以正常响应,反之却不可以;
最简单的理解,因为调用槽函数的参数缺一不可
*/

综上所述: Qt4的连接方式最大的缺点就是编译时无法报错,在运行期间才会验证是否正确,这对于C++ 这种静态语言,简直就是毁灭

Qt5 仿函数写法(强推,务必)

signals:
    void testMocSignal(int in1,int in2);
    void testMocSignal2();
    void testMocSignal3(QString str);
protected slots:
   void DoCloseSlot(int in1,int in2);
protect:
   void DoCloseSlot2();
   void DoCloseSlot3(int in);
//连接成功
connect(this, &TestMoc::testMocSignal, this, &TestMoc::DoCloseSlot);
//连接失败,参数个数不匹配
connect(this, &TestMoc::testMocSignal2, this, &TestMoc::DoCloseSlot);
//连接失败,参数类型不匹配
connect(this, &TestMoc::testMocSignal3, this, &TestMoc::DoCloseSlot3);
//连接失败,参数个数不匹配
connect(this, &TestMoc::testMocSignal, this, [this](){
    qDebug()<< __FUNCTION__ ;
});
//连接成功
connect(this, &TestMoc::testMocSignal, this, [this](int in1,int in2){
    qDebug()<< __FUNCTION__ << in1 << in2;
});
//连接成功,带有默认参数
connect(this, &TestMoc::testMocSignal, this, [this](int in1, int in2,int in3=999) {
        qDebug() << __FUNCTION__<<in1<<in2<<in3;
});

直接摆优点:

  • 严格的信号槽参数匹配校验,可以在编译期就避免很多问题
  • 槽函数不在限定必须是slot,可以是普通函数,类成员函数,甚至是lambda表达式
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值