Mapping signals and slots

Have you ever hoped there was a class that was able to do the opposite of QSignalMapper? For example, “If the signal parameter is 2, call that slot of this receiver. If the signal parameter is “foo”, call this slot of that receiver.” We have it now in Qxt, it’s called QxtSlotMapper.

An example follows:

QxtSlotMapper mapper;

// mapping with integers
mapper.addMapping(123, someWidget, SLOT(show()));
mapper.addMapping(456, someWidget, SLOT(hide()));

// mapping with strings
mapper.addMapping("lower", anotherWidget, SLOT(lower()));
mapper.addMapping("raise", anotherWidget, SLOT(raise()));

// establish connections
mapper.connect(listWidget, SIGNAL(currentRowChanged(int)));
mapper.connect(lineEdit, SIGNAL(textEdited(QString)));

The comparison is based on QVariant::operator==(), so it basically works with any built-in Qt value type.

Leave a Reply