
Originally Posted by
MarkoSan
Does every button in a QList <QPushButton*> need its own QSIgnalMapper?
No, you need only one signal mapper for the whole list.
In Qt docs you can find such example:
...
for (int i = 0; i < texts.size(); ++i) {
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
...
}
connect(signalMapper,
SIGNAL(mapped
(const QString &)),
this,
SIGNAL(clicked
(const QString &)));
...
...
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
...
}
connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SIGNAL(clicked(const QString &)));
...
To copy to clipboard, switch view to plain text mode
The setMapping() line tells the signal mapper that whenever the particular button invokes map(), the signal mapper should emit mapped(const QString &) signal with given parameter. So in this case you can replace all clicked() signals coming from different buttons with a single mapped( const QString & ) signal that comes from the signal mapper (of course parameter value depends on which button sent the original signal).
You can have a mapped() signal also with integer, QObject * or QWidget * parameter --- you just have to use the other setMapping() method variants instead.
In your case you can use QButtonGroup as jpn has suggested. Basically it's like a QSignalMapper but crafted especially for buttons.
Bookmarks