[Solved] Differentiating multiple signals going to a single slot
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]->resize(60, 40);
- 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 (pushButton0, pushButton6, etc.)?
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.