• 注册
当前位置:1313e > 默认分类 >正文

【QT】 QT实现一个信号与多个槽的关联和实现多个信号与一个槽的关联

       

这个问题很简单,我们定义一个按钮就是一个信号,而相应的事件就是一个槽。

而这里用到的方法就是connect。
connect的两个实例如下:

connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(FoodIsComing()));    connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(FoodIsComing()));    connect(ui->pushButton_5,SIGNAL(clicked()),this,SLOT(FoodIsComing()));
  • 1
  • 2
  • 3

这个就是多个信号对应的一个槽。

给出的一个代码如下:

#include "widget.h"#include "ui_widget.h"#include #include Widget::Widget(QWidget *parent) :    QWidget(parent),    ui(new Ui::Widget){    ui->setupUi(this);    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(FoodIsComing()));    connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(FoodIsComing()));    connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(FoodIsComing()));    connect(ui->pushButton_5,SIGNAL(clicked()),this,SLOT(FoodIsComing())); //   connect(ui->lineEdit,SIGNAL(textEdited(QString)),this,SLOT(PrintText(QString)));}Widget::~Widget(){    delete ui;}void Widget::FoodIsComing(){   QString get = this->sender()->objectName();   qDebug()<<get; //打印源头对象名称   QString strMsg;   if("pushButton_3" == get){       strMsg = "hello,welcome ,老王";   }   else if("pushButton_4" == get){       strMsg = "hello,welcome ,老李";   }   else if("pushButton_5" == get){       strMsg = "hello,welcome ,老刘";   }   else{       return ;   }   //显示送餐消息   QMessageBox::information(this,tr("food"),strMsg);}void Widget::on_pushButton_2_clicked()     //我饿了{    QMessageBox::information(this,tr("餐吃完了"),tr("注意,我吃饱了"));}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

把三个信号关联到了一个槽里面,然后通过槽获得对象名,然后解析成不同的字符表达出来

通过这句话能够解析出名字:
 QString get = this->sender()->objectName();
输出结果:
这里写图片描述

           

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录