版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23100787/article/details/51162944
这个问题很简单,我们定义一个按钮就是一个信号,而相应的事件就是一个槽。
而这里用到的方法就是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()));
这个就是多个信号对应的一个槽。
给出的一个代码如下:
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QDebug>
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("注意,我吃饱了"));
}
把三个信号关联到了一个槽里面,然后通过槽获得对象名,然后解析成不同的字符表达出来
通过这句话能够解析出名字:
QString get = this->sender()->objectName();
输出结果: