Signal Mapper

A signal mapper is a useful tool when we need to perform almost the same action for many signals. Instead of checking the "caller" in the slot, and making the decision based on that, we can create a "mapping" between a signalling object and some identifying parameter. Now, our handling slot takes the id parameter and makes its decision based on that.
Using a signal mapper can save us subclassing a QPushButton for an id. Here's a short usage example.

#include <QtGui>

int main(int argc, char **argv)
    QApplication app(argc, argv);

    QWidget w;;

    QLCDNumber *display = new QLCDNumber(&w);

    QHBoxLayout *btnsLayout = new QHBoxLayout();
    QVBoxLayout *layout     = new QVBoxLayout();


    QSignalMapper *mapper = new QSignalMapper(&w);
    QObject::connect(mapper, SIGNAL(mapped(QString)), display, SLOT(display(QString)));

    for ( int i=0; i < 3; ++i)
        QPushButton *b = new QPushButton(QString("Button: %1").arg(i));

        QObject::connect(b, SIGNAL(clicked()), mapper, SLOT(map()));
        mapper->setMapping(b, QString("%1").arg(i));

    w.resize(120, 120);