多信号单槽区分信号来源

在slot中获取sender,转换为要判断的类型。然后进行处理。

QObject *object = QObject::sender();
 QPushButton *push_button = qobject_cast<QPushButton *>(object);

 

 

 

 

以下转至 原文:https://blog.youkuaiyun.com/u014339020/article/details/81016424 

在多个信号绑定到同一个槽函数的状态下,让槽函数根据绑定的不同的信号执行不同的功能有两种方法:

(1)在槽函数中获取到sender对象,反向解析出信号的名称,分叉执行;

(2)使用QSingalMapper类:管理多个信号,槽函数的处理相对变得简单。

具体来看:

(1)反向获取Sender名称

关键函数:

QObject::sender()                 // 在槽函数中获取信号
QObject::setObjectName(QString)    // 给QObject对象设置名称
QObject::objectName()              // 获取QObject对象名称
QObject_cast<QObject>(object)      // 强转对象类型
思路如下:

示例代码如下:

typedef enum{
BUTTON_1,
BUTTON_2,
BUTTON_3,
BUTTON_4
}BUTTON;
 
 push_button_1->setObjectName(QString::number(BUTTON_1, 10));
 push_button_2->setObjectName(QString::number(BUTTON_2, 10));
 tool_button_1->setObjectName(QString::number(BUTTON_3, 10));
 tool_button_2->setObjectName(QString::number(BUTTON_4, 10));
 connect(push_button_1, &QPushButton::clicked, this, &MyWidget::changeButton);
 connect(push_button_2, &QPushButton::clicked, this, &MyWidget::changeButton);
 connect(tool_button_1, &QToolButton::clicked, this, &MyWidget::changeButton);
 connect(tool_button_2, &QToolButton::clicked, this, &MyWidget::changeButton);
 
void MyWidget::changeButton()
{
    QObject *object = QObject::sender();
    QPushButton *push_button = qobject_cast<QPushButton *>(object);
    QToolButton *tool_button = qobject_cast<QToolButton *>(object);
    int index;
    if(push_button)
    {
        QString object_name = push_button->objectName();
        index = object_name.toInt();
    }
    else if(tool_button )
    {
         QString object_name = tool_button->objectName();
         index = object_name.toInt();
    }
 
    QString information = QString("");
    switch(index)
    {
    case BUTTON_1:
        information = QString("clicked 1");
        break;
 
    case BUTTON_2:
        information = QString("clicked 2");
        break;
 
    case BUTTON_3:
        information = QString("clicked 3");
        break;
 
    case BUTTON_4:
        information = QString("clicked 4");
        break;
 
    default:
        information = QString("which is clicked?");
        break;
    }
    QMessageBox::information(NULL, QString("Title"), information);
}
( 2 )  使用QSignalMapper类

这个思想是:希望能够在信号关联中直接传递一个参数!直接用信号槽无法实现

QSignalMapper类内置了一个Map表,将Singnal和参数对应起来,然后多个信号关联到Mapper上,由mapper负责管理,并且mapper关联到槽函数中,将对应的参数传入槽函数

这个流程图如下:

实例代码如下:

QSignalMapper *signal_mapper = new QSignalMapper(this);
connect(push_button_1, &QPushButton::clicked, signal_mapper, &QSignalMapper::map);
connect(push_button_2, &QPushButton::clicked, signal_mapper, &QSignalMapper::map);
connect(tool_button_1, &QToolButton::clicked, signal_mapper, &QSignalMapper::map);
connect(tool_button_2, &QToolButton::clicked, signal_mapper, &QSignalMapper::map);
 
signal_mapper->setMapping(push_button_1, QString::number(BUTTON_1, 10));
signal_mapper->setMapping(push_button_2, QString::number(BUTTON_2, 10));
signal_mapper->setMapping(tool_button_1, QString::number(BUTTON_3, 10));
signal_mapper->setMapping(tool_button_2, QString::number(BUTTON_4, 10));
connect(signal_mapper, &QSignalMapper::mapped, this, &MyWidget::changeButton);
 
void MyWidget::changeButton(QString text)
{
    int index = text.toInt();
    QString information = QString("");
    switch(index)
    {
    case BUTTON_1:
        information = QString("clicked 1");
        break;
 
    case BUTTON_2:
        information = QString("clicked 2");
        break;
 
    case BUTTON_3:
        information = QString("clicked 3");
        break;
 
    case BUTTON_4:
        information = QString("clicked 4");
        break;
 
    default:
        information = QString("which is clicked?");
        break;
    }
    QMessageBox::information(NULL, QString("Title"), information);
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值