Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[Solved] Differentiating multiple signals going to a single slot
Endless last edited by
Here's some code that creates a list of 10 pushbuttons and connects the clicked singal of all of them to a single slot (on_pushButtonList_clicked)
for(index = 0; index < 10; index++)
pushButton[index] = new QPushButton(this);
pushButton[index]->move(400, 50 + 50 * index);
pushButton[index]->setText(QString::number(index, 'g', 2));
connect(pushButton[index], SIGNAL(clicked()), this, SLOT(on_pushButtonList_clicked()));
When I get to the on_pushButtonList_clicked slot, how do I find out which button was clicked? In addition to getting a pointer to the button that was hit, is there a way to get its index position (pushButton, pushButton, etc.)?
Check out "QSignalMapper.":http://qt-project.org/doc/qt-4.8/qsignalmapper.html
tolszak last edited by
You can get pointer to pushbutton in slot by using QObject::sender() method.
If you have access to pushButtonList (I suppose it's QList) you can get button index with
If you don't have access to pushButtonList from slot you should check QSignalMapper as mlong suggested.