I am now trying the following: passing sender()->objectName().toInt() of a QPushButton in the systembutton_clicked(..., ..., ...) function. However, the program crashes on click of btnTest. So how can I let the program know which btnTest is clicked so a button can be added to the right groupBox? How to get it right so it not crashes?


Qt Code:
  1. void MyWidget::systembutton_clicked(int j, QGroupBox *groupBox, QVBoxLayout *vbox)
  2. {
  3.  
  4. systemcounter[j] += 1;
  5. QLayoutItem *child;
  6. while ((child = vbox->takeAt(0)) != 0){
  7. delete child;
  8. }
  9. for(int i = 0; i<systemcounter[j]; 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. systemcounter = new int[pagecounter];
  24. systemcounter[pagecounter-1] = 0;
  25.  
  26. QGroupBox *groupBox = new QGroupBox(tr("&Page %1").arg(pagecounter));
  27. QPushButton *btnTest = new QPushButton("G", this);
  28. btnTest->setObjectName(QString::number(pagecounter-1));
  29.  
  30. top->addWidget(groupBox);
  31. top->addWidget(btnTest);
  32. top->maximumSize();
  33.  
  34. mainLayout->addLayout(top);
  35.  
  36. QVBoxLayout *vbox = new QVBoxLayout;
  37. groupBox->setLayout(vbox);
  38. connect(btnTest, &QPushButton::clicked, [=] {
  39. emit systembutton_clicked(sender()->objectName().toInt(), groupBox, vbox);
  40. });
  41. }
To copy to clipboard, switch view to plain text mode