How does QSignalMapper work?

前端 未结 2 1455
栀梦
栀梦 2020-12-28 09:27

After my post here : Associate signal and slot to a qcheckbox create dynamically I need to associate :

• The signal clicked() when I click on a qC

2条回答
  •  难免孤独
    2020-12-28 10:29

    QSignalMapper class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal. So you can have one like:

    QSignalMapper * mapper = new QSignalMapper(this);
    QObject::connect(mapper,SIGNAL(mapped(QWidget *)),this,SLOT(mySlot(QWidget *)));
    

    For each of your buttons you can connect the clicked() signal to the map() slot of QSignalMapper and add a mapping using setMapping so that when clicked() is signaled from a button, the signal mapped(QWidget *) is emitted:

    QPushButton * but = new QPushButton(this);
    
    QObject::connect(but, SIGNAL(clicked()),mapper,SLOT(map()));
    mapper->setMapping(but, but);
    

    This way whenever you click a button, the mapped(QWidget *) signal of the mapper is emitted containing the widget as a parameter.

提交回复
热议问题