Working around the signal and slot argument limitation


I want to make a PushButton when it is clicked, its text change into 'clicked'. I tried it by

connect(button1, SIGNAL(clicked()), this, SLOT(markClicked(button1)));

where this refer to the MainWindow and

void MainWindow::markClicked(QPushButton *button) { button->setText("Clicked"); }

It does not seem to work because I think SLOT cannot take more arguments than SIGNAL. If there any approach to work around this limitation?


Qt signals/slots mechanism can only transfer signal to slot function with similar parameters. As a workaround, you should use QSignalMapper:

QSignalMapper mapper;
connect(button1, SIGNAL(clicked()), &mapper, SLOT(map()));
mapper.setMapping(button1, button1); // not sure whether this is mandatory or not
connect(&mapper, SIGNAL(mapped(QWidget*)), this, SLOT(markClicked(QWidget*)));

and function markClicked is

void MainWindow::markClicked(QWidget *widget) {
  QPushButton *button = qobject_cast<QPushButton*>(widget);