QSignalMapper use and usage scenarios

QSignalMapper use and usage scenarios

QSignalMapper class collects a series of non-reference signal, and then use the signal with respect to the sender is an integer, string, or send them back to the control parameters. (I did not understand it does not matter, to understand the reading)

Common scenarios

In fact, the class of a typical usage scenarios is that many of the controls should function corresponding slots, and slots to achieve these functions and roughly the same. In this case, the most direct way is still to create a slot for the corresponding function of each control signal. But this can lead to a lot of duplicate code. At this point, we can use QSignalMapper to implement this requirement.

  1. QSignalMapper classes support SetMapping () function to a particular integer or string, and associate a particular object.

void QSignalMapper::setMapping

  1. Signal (such as the clicked button) may be connected to the object QSignalMapper object map () function on the groove, and the map () function will use slot associated with the object to transmit the mapped string or an integer of () signal.

  2. Therefore, as long as we we define a groove connected to a mapped function () signal, a large number of similar function to control the processing of the groove.

We have an example to illustrate. Its interface is as follows: (Examples of the second article from csdn, slight additional modifications)

Please see the supplementary notes carefully. It will be written above the corresponding three.
Interface initialization code is as follows:

void Widget::InitUi()
{
    names << "宋江" << "卢俊义" << "吴用" << "公孙胜"
          << "关胜" << "林冲" << "秦明" << "呼延灼"
          << "花荣" << "柴进" << "李应" << "朱仝"
          << "鲁智深" << "武松" << "董平" << "张清";
    QGridLayout *gridLayout = new QGridLayout;
    for (int i = 0; i < names.size(); ++i)
    {
        QPushButton *button = new QPushButton(names[i]);
        // 使用setMapping()函数将一个特定的整数或字符串或对象(qt doc截图那些)和一个特定的对象关联起来。
        signalMapper->setMapping(button, names[i]);
        // 这就是可以将对象的信号连接到QSignalMapper对象的map()槽函数上
        connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
        gridLayout->addWidget(button, i / 4, i % 4);
    }
    // 将我们定义的槽函数连接到mapped()信号 // 注意看mapped的写法
    connect(signalMapper, SIGNAL(mapped(QString)),this, SLOT(ShowName(QString)));
    setLayout(gridLayout);
}

Which, names QStringList is a private variable to store the text on each button. ShowName () is a function we define the groove, so that we are all clicked the button () signal is connected to the slot function. In the window class the following statement:

public slots:
    void ShowName(QString name);
 
private:
    void InitUi();
 
private:
    Ui::Widget *ui;
    QSignalMapper* signalMapper;
    QStringList names;

ShowName () function is achieved as the groove, a simple pop-up message box, text displays the current click button:

void Widget::ShowName(QString name)
{
    QMessageBox::information(this, "Name", name);
}

Of course, do not forget in the constructor, call our initialization interface methods, and examples of our signalMapper object. as follows:

ui->setupUi(this);
    signalMapper = new QSignalMapper(this);
    InitUi();

The following is a reference. See do not see

This well written, moving to a Markdown can look at

QSignalMapper class can be seen as a signal repeater and translator.
It can be a non-reference signal into translated parameters with int, QString signal parameters, QObject * QWidget * parameter or parameters, and the forwarding.
QSignalMapper functional core classes is to establish a mapping from the object to the desired original signal data (setMapper function). map () function as a slot QSignalMapper will forward mapped () signal in accordance with the rules setMapping.
QSignalMapper may be a plurality of similar treatment with a slot to achieve signal, corresponding to the N-one mapping by converting concentrated into one mapping.

Example: There are a bunch of button, can be clicked () event in a function in processing, as long as the button to compile a number or a name to the button on the line, so do not write a function to each button slot, and a lot easier.

//mywidget.h
class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = 0);

signals:

public slots:
    //处理最终信号的槽
    void doClicked(const QString &btnName);

private:
    QSignalMapper *signalMapper;

};
//mywidget.cpp
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    QString buttonText = "btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10";
    QStringList textList = buttonText.split(",");
    signalMapper = new QSignalMapper(this);
    QGridLayout *gridLayout = new QGridLayout;

    for(int i=0; i<textList.size(); ++i)
    {
        //动态创建按钮
        QPushButton *button = new QPushButton(textList[i]);

        //原始信号传递给signalMapper
        connect(button,SIGNAL(clicked()),signalMapper,SLOT(map()));
        //设置signalMapper的转发规则,转发为参数为QString类型的信号,并把textList[i]的内容作为实参传递
        signalMapper->setMapping(button, textList[i]);

        gridLayout->addWidget(button, i/3, i%3);
    }

    //将转发的信号连接到最终的槽函数上
    connect(signalMapper,SIGNAL(mapped(QString)),this,SLOT(doClicked(QString)));

    setLayout(gridLayout);
}

void MyWidget::doClicked(const QString &btnName)
{
    //显示被按下的button名称
    QMessageBox::information(this,"Clicked",btnName+" is clicked !");
}

Examples:
First, the raw signal with no parameters is connected to the map () function signalMapper groove, so signalMapper can receive the original signal at a first time;
Second method of mapping relationship setMapper call, telling how to process the raw signals signalMapper. This example is the conversion of the original signal with a signal QString parameter;
with the parameters of the last received signal conversion, the converted signal where the slot function and connected to obtain the desired data in the slot function

Mapping relationship may () removed by removeMappings;
parameter only four setMapping function, and to write in strict accordance with the format, a first const QString &, second int, third QObject *, fourth QWidget *, for the latter two, require their sub-class, the type of conversion in the signal processing function.

reference

https://doc.qt.io/qt-5/qsignalmapper.html#details

https://blog.csdn.net/Amnes1a/article/details/70050788

https://blog.csdn.net/u011125673/article/details/51218196

https://blog.csdn.net/Amnes1a/article/details/70050788

Guess you like

Origin www.cnblogs.com/__tudou__/p/11586454.html