[Qt Foundation-07 QSignalMapper]

QSignalMapper

This article mainly introduces the function and use of QSignalMapper based on the official QT help documentation and daily use.


Introduction

QSignalMapper is a class that binds known emitter objects and signals together. By looking at the content in the help documentation, its main function is to bind a parameterless signal to a parameter, and then forward this signal plus this parameter.

The application scenario is generally: you have some signals, and the slot functions corresponding to these signals have similar contents. The easiest way is to connect a slot function to each signal. But doing so will make the code unnecessarily complicated. In this case, you can use QSignalMapper

Instructions

According to the example in the official documentation, the method used is:

QSignalMapper *mapper = new QSignalMapper(this);
QStringList buttonText = {
    
    "button", "button_2", "button_3"};

for (int i = 0; i < buttonText.size(); i++) {
    
    
    QPushButton *button = new QPushButton(this);
    // 1. 把要转发的信号和QSignalMapper的map信号进行连接
    connect(button, &QPushButton::clicked, mapper, &QSignalMapper::map);
    // 2. 设置信号的映射关系
    mapper->setMapping(button, buttonText.at(i));
}

// 3. 将QSignalMapper的转发信号连接起来
connect(mapper, QOverload<const QString &>::of(&QSignalMapper::mapped),
        [const QString &text] () {
    
    
            qDebug() << text;
        });

This method was indeed a good method before QT introduced lambda as a slot function. But after lambda is introduced, it can be written in lambda function as follows, which is more convenient.

QSignalMapper *mapper = new QSignalMapper(this);
QStringList buttonText = {
    
    "button", "button_2", "button_3"};

for (int i = 0; i < buttonText.size(); i++) {
    
    
    QString text = buttonText.at(i);
    QPushButton *button = new QPushButton(this);
    connect(button, &QPushButton::clicked, [=] () {
    
    
        qDebug() << text;
    });
}

main function

  1. setMapping (set the mapping relationship between objects and forwarding parameters)

    The prototype of this function is:

    void setMapping(QObject *sender, int id);
    void setMapping(QObject *sender, QString text);
    void setMapping(QObject *sender, QWidget *widget);
    void setMapping(QObject *sender, QObject *object);
    

    The function is to set the mapping relationship between the object and the forwarding parameters, that is, to set the parameters of the retransmitted signal after the signal of the object is forwarded.

  2. removeMappings (delete the mapping relationship of the specified object)

    The prototype of this function is:

    void removeMappings(QObject* sender);
    

    The function is to remove the mapping relationship of the specified object signal.

  3. mapping (get the mapping relationship between objects and forwarding parameters)

    The function prototype is:

    QObject * mapping(int id) const
    QObject * mapping(const QString &id) const
    QObject * mapping(QWidget *widget) const
    QObject * mapping(QObject *object) const
    

    The function of this function is to obtain the corresponding object according to the forwarded parameters.

Signals and slots

slot function

  1. map

    The function prototype is:

    void map(QObject *sender)
    void map()
    

    The map function with parameters will emit the signal mapped by the sender object. The map function without parameters will emit the corresponding signal with parameters according to the signal connected to the slot function.

Signal

  1. mapped

    The prototype of the function is:

    void mapped(QObject *object)
    void mapped(QWidget *widget)
    void mapped(const QString &text)
    void mapped(int i)
    

    This signal is based on the parameters bound to the object to determine the parameters of the emitted signal .
    Because this signal is an overloaded function, if you need to use a function pointer to connect the signal:

    connect(mapper, QOverload<const QString &>::of(&QSignalMapper::mapped), 
           [const QString &text] () {
          
          
               qDebug() << text;
           });
    

Guess you like

Origin blog.csdn.net/qq_44723937/article/details/126004709