Qt多个信号关联同一个槽函数
背景:多个信号需要执行同一个函数或者一类函数的时候,可以选择每个信号创建一个槽函数去实现功能,如果直接关联到一个函数中,该函数只能执行一份功能,有时候并不能满足业务需求
在多个信号绑定到同一个槽函数的状态下,让槽函数根据绑定的不同的信号执行不同的功能有两种方法:
(1)在槽函数中获取到sender对象,反向解析出信号的名称,分叉执行;
(2)使用QSingalMapper类:管理多个信号,槽函数的处理相对变得简单。
具体来看:
(1)反向获取Sender名称
关键函数:1QObject::sender()在槽函数中获取信号2QObject::setObjectName(QString)给QObject对象设置名称3QObject::objectName()获取QObject对象名称4QObjectcastQObject(object)强转对象类型
思路如下:
示例代码如下:1typedefenum{2BUTTON1,3BUTTON2,4BUTTON3,5BUTTON46}BUTTON;78pushbutton1setObjectName(QString::number(BUTTON1,10));9pushbutton2setObjectName(QString::number(BUTTON2,10));10toolbutton1setObjectName(QString::number(BUTTON3,10));11toolbutton2setObjectName(QString::number(BUTTON4,10));12connect(pushbutton1,QPushButton::clicked,this,MyWidget::changeButton);13connect(pushbutton2,QPushButton::clicked,this,MyWidget::changeButton);14connect(toolbutton1,QToolButton::clicked,this,MyWidget::changeButton);15connect(toolbutton2,QToolButton::clicked,this,MyWidget::changeButton);1617voidMyWidget::changeButton()18{19QObjectobjectQObject::sender();20QPushButtonpushbuttonqobjectcastQPushButton(object);21QToolButtontoolbuttonqobjectcastQToolButton(object);22intindex;23if(pushbutton)24{25QStringobjectnamepushbuttonobjectName();26indexobjectname。toInt();27}28elseif(toolbutton)29{30QStringobjectnametoolbuttonobjectName();31indexobjectname。toInt();32}3334QStringinformationQString();35switch(index)36{37caseBUTTON1:38informationQString(clicked1);39break;4041caseBUTTON2:42informationQString(clicked2);43break;4445caseBUTTON3:46informationQString(clicked3);47break;4849caseBUTTON4:50informationQString(clicked4);51break;5253default:54informationQString(whichisclicked?);55break;56}57QMessageBox::information(NULL,QString(Title),information);58}
【领QT开发教程学习资料,点击下方链接莬费领取,先码住不迷路】
点击领取链接(2)使用QSignalMapper类
这个思想是:希望能够在信号关联中直接传递一个参数!直接用信号槽无法实现
QSignalMapper类内置了一个Map表,将Singnal和参数对应起来,然后多个信号关联到Mapper上,由mapper负责管理,并且mapper关联到槽函数中,将对应的参数传入槽函数
这个流程图如下:
实例代码如下:1QSignalMappersignalmappernewQSignalMapper(this);2connect(pushbutton1,QPushButton::clicked,signalmapper,QSignalMapper::map);3connect(pushbutton2,QPushButton::clicked,signalmapper,QSignalMapper::map);4connect(toolbutton1,QToolButton::clicked,signalmapper,QSignalMapper::map);5connect(toolbutton2,QToolButton::clicked,signalmapper,QSignalMapper::map);67signalmappersetMapping(pushbutton1,QString::number(BUTTON1,10));8signalmappersetMapping(pushbutton2,QString::number(BUTTON2,10));9signalmappersetMapping(toolbutton1,QString::number(BUTTON3,10));10signalmappersetMapping(toolbutton2,QString::number(BUTTON4,10));11connect(signalmapper,QSignalMapper::mapped,this,MyWidget::changeButton);1213voidMyWidget::changeButton(QStringtext)14{15intindextext。toInt();16QStringinformationQString();17switch(index)18{19caseBUTTON1:20informationQString(clicked1);21break;2223caseBUTTON2:24informationQString(clicked2);25break;2627caseBUTTON3:28informationQString(clicked3);29break;3031caseBUTTON4:32informationQString(clicked4);33break;3435default:36informationQString(whichisclicked?);37break;38}39QMessageBox::information(NULL,QString(Title),information);40}4120articledetails81016424