I have the following functions inside a childwindow. When onaddbutton_clicked() is triggered, a new QGroupBox and QPushbutton are added. Onclick of such a QPushButton, a button inside the QGroupBox must be added. But the QPushButtons must work independently from each other. Now systemcounter just keeps on increasing, causing buttons to be added inside all existing QGroupBoxes. I thought of making a systemcounter array, but then how to use the right systemcounter from the array? Also, is the QSignalMapper signalMapper working at all?
So how to make the QPushbuttons work independently from each other?

Qt Code:
  1. void MyWidget::systembutton_clicked(QGroupBox *groupBox, QVBoxLayout *vbox, int k)
  2. {
  3. systemcounter += 1;
  4.  
  5. QLayoutItem *child;
  6. while ((child = vbox->takeAt(0)) != 0){
  7. delete child;
  8. }
  9. for(int i = 0; i<systemcounter; i++){
  10. QPushButton *btnGtr = new QPushButton();
  11. btnGtr->setText(QString("Guitar: %1").arg(i+1));
  12. vbox->addWidget(btnGtr);
  13. QObject::connect(btnGtr, SIGNAL (clicked()), this, SLOT (handleButton()));
  14. }
  15.  
  16. mainLayout->addWidget(groupBox);
  17.  
  18. }
  19.  
  20. void MyWidget::onaddbutton_clicked()
  21. {
  22. pagecounter += 1;
  23.  
  24. QGroupBox *groupBox = new QGroupBox[pagecounter];
  25. QPushButton *btnTest = new QPushButton("G", this);
  26.  
  27. top->addWidget(groupBox);
  28. top->addWidget(btnTest);
  29. top->maximumSize();
  30.  
  31. mainLayout->addLayout(top);
  32.  
  33. QVBoxLayout *vbox = new QVBoxLayout;
  34. groupBox->setLayout(vbox);
  35. signalMapper = new QSignalMapper();
  36. for(int i = 0; i<pagecounter; i++){
  37. connect(btnTest, &QPushButton::clicked, [=] {
  38. emit systembutton_clicked(&groupBox[i], vbox, i);
  39. });
  40. signalMapper->setMapping(btnTest, i);
  41. }
  42. }
To copy to clipboard, switch view to plain text mode