这个问题很简单,我们定义一个按钮就是一个信号,而相应的事件就是一个槽。
而这里用到的方法就是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();
输出结果: