QT之QSignalMapper(可以理解为转发器,多个按钮绑定到一个Edit上,且能分辨。每个单独连接的话,反而麻烦)

QT之QSignalMapper

简述

QSignalMapper我们可以理解为转发器,此话怎讲呢?比如,按钮点击的响应槽,绑定到QSignalMapper上,QSignalMapper收到按钮的点击后,又通知到另外的控件上做处理。有的朋友会问了,为什么要这么麻烦,需要转一手,不能去掉中间的QSignalMapper,而直接调用吗。答案当然是可以的,为什么需要QSignalMapper这个转发器呢,我们先来看下效果,然后再看看代码,相信大家会喜欢上QSignalMapper这个转发器的。

效果图

QSignalMapper

上代码

void QSignalMapper_Demo::initControl()
{
    QVBoxLayout *pLayout = new QVBoxLayout(this);

    QString str = "Button1 Button2 Button3 Button4 Button5";
    QStringList strList = str.split(" ");
    QSignalMapper *pMapper = new QSignalMapper(this);

    int nRowCnt = 0;
    foreach(QString itor, strList)
    {
        QPushButton *pBtn = new QPushButton(this);
        pBtn->setFixedHeight(CON_HEGIHT);
        pBtn->setText(itor);

        connect(pBtn, SIGNAL(clicked()), pMapper, SLOT(map()));
        pMapper->setMapping(pBtn, pBtn->text());

        pLayout->addWidget(pBtn, nRowCnt++, 0);
    }

    QLineEdit *pEdit = new QLineEdit(this);
    pEdit->setFixedHeight(CON_HEGIHT);
    connect(pMapper, SIGNAL(mapped(QString)), pEdit, SLOT(setText(QString)));
    pLayout->addWidget(pEdit, nRowCnt, 0);
    pLayout->addStretch();
}
  • 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

眼光利索的朋友看到了我代码中,代码不规范,说你代码怎么就new,怎么没deltete,你这样不会内存泄漏吗,这里告诉大家是不会的哦,相关知识可以看我之前写的QT之内存泄漏管理

回到本节,看完QSignalMapper的用法,我们回过头再看看,不用QSignalMapper,又该如何实现上面的效果呢,编辑框中我们可以看到,是哪个按钮被按下了。

//去掉QSignalMapper
void QSignalMapper_Demo::initControl()
{
    QVBoxLayout *pLayout = new QVBoxLayout(this);

    QString str = "Button1 Button2 Button3 Button4 Button5";
    QStringList strList = str.split(" ");

    int nRowCnt = 0;
    foreach(QString itor, strList)
    {
        QPushButton *pBtn = new QPushButton(this);
        pBtn->setFixedHeight(CON_HEGIHT);
        pBtn->setText(itor);
        //这里,响应的是同一个槽,按钮被点击后,我们却不能知道是哪个按钮被按下。
        connect(pBtn, SIGNAL(clicked()), this, SLOT(onclicked()()));
        //有的朋友说,可以每个按钮绑定不同的槽做区分呀,这样做可以是可以,但是你觉得使用QSignalMapper是不是要更简洁些呢?
        pLayout->addWidget(pBtn, nRowCnt++, 0);
    }

    QLineEdit *pEdit = new QLineEdit(this);
    pEdit->setFixedHeight(CON_HEGIHT);
    pLayout->addWidget(pEdit, nRowCnt, 0);
    pLayout->addStretch();
}
  • 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

看完代码,相信大家都知道了QSignalMapper的美!有的朋友又问了,我什么时候该用QSignalMapper呢?通过上面的例子很简单呀,我们需要处理同一个事情时,而简化多个不同的槽却做着相同的事,我们的QSignalMapper就派上用场啦!

相关知识点文章

QT之内存泄漏管理

结尾

http://blog.csdn.net/ly305750665/article/details/53790209

原文地址:https://www.cnblogs.com/findumars/p/8035496.html