I have QList of QPushButton‘s and QSignalMapper to recognize which button is pushed.
So I did something like that (my project is very big, so I cut just the lines needed to the question)
QList<QPushButton*> Buttons;
QList <QLabel*> LabelList1;
QList <QLabel*> LabelList2;
QList <QLabel*> LabelList3;
QList <QLabel*> LabelList4;
QSignalMapper *ButtonsMapper;
ButtonsMapper= new QSignalMapper(this);
connect(ButtonsMapper, SIGNAL(mapped(int)),this,SIGNAL(ButtonsClicked(int)));
connect(this, SIGNAL(ButtonsClicked(int)),this,SLOT(deleteButton(int)));
Buttons.append(new QPushButton(tr("0")));//first button
LabelList1.append(new QLabel(tr("0")));
LabelList2.append(new QLabel(tr("0")));
LabelList3.append(new QLabel(tr("0")));
LabelList4.append(new QLabel(tr("0")));
QPushButton * pb1 = Buttons.last();//pointer to the last button
connect(pb1, SIGNAL(clicked()), ButtonsMapper, SLOT(map()));
ButtonsMapper->setMapping(pb1,0);
Buttons.append(new QPushButton(tr("1")));//second button
LabelList1.append(new QLabel(tr("1")));
LabelList2.append(new QLabel(tr("1")));
LabelList3.append(new QLabel(tr("1")));
LabelList4.append(new QLabel(tr("1")));
QPushButton * pb2 = Buttons.last();//pointer to the last button
connect(pb2, SIGNAL(clicked()), ButtonsMapper, SLOT(map()));
ButtonsMapper->setMapping(pb2,1);
and the function deleteButton supposed to delete the button was pressed. If I do something like that
void myclass::deleteButton(int i){
delete (Buttons.takeAt(i));
delete ( LabelList1.takeAt(i));
delete ( LabelList2.takeAt(i));
delete( LabelList3.takeAt(i));
delete( LabelList4.takeAt(i));
}
that function can lead to index out of range error, if I delete the first button, and then I press on the second button, the function Buttons.takeAt(i) point to not exist button.
Theoretically, you could keep a pointer to the signal mapper around, and remap the indexes after deletion.