bchinfosieeuw
23rd December 2016, 15:26
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?
void MyWidget::systembutton_clicked(QGroupBox *groupBox, QVBoxLayout *vbox, int k)
{
systemcounter += 1;
QLayoutItem *child;
while ((child = vbox->takeAt(0)) != 0){
delete child;
}
for(int i = 0; i<systemcounter; i++){
QPushButton *btnGtr = new QPushButton();
btnGtr->setText(QString("Guitar: %1").arg(i+1));
vbox->addWidget(btnGtr);
QObject::connect(btnGtr, SIGNAL (clicked()), this, SLOT (handleButton()));
}
mainLayout->addWidget(groupBox);
}
void MyWidget::onaddbutton_clicked()
{
pagecounter += 1;
QHBoxLayout *top = new QHBoxLayout;
QGroupBox *groupBox = new QGroupBox[pagecounter];
QPushButton *btnTest = new QPushButton("G", this);
top->addWidget(groupBox);
top->addWidget(btnTest);
top->maximumSize();
mainLayout->addLayout(top);
QVBoxLayout *vbox = new QVBoxLayout;
groupBox->setLayout(vbox);
signalMapper = new QSignalMapper();
for(int i = 0; i<pagecounter; i++){
connect(btnTest, &QPushButton::clicked, [=] {
emit systembutton_clicked(&groupBox[i], vbox, i);
});
signalMapper->setMapping(btnTest, i);
}
}
So how to make the QPushbuttons work independently from each other?
void MyWidget::systembutton_clicked(QGroupBox *groupBox, QVBoxLayout *vbox, int k)
{
systemcounter += 1;
QLayoutItem *child;
while ((child = vbox->takeAt(0)) != 0){
delete child;
}
for(int i = 0; i<systemcounter; i++){
QPushButton *btnGtr = new QPushButton();
btnGtr->setText(QString("Guitar: %1").arg(i+1));
vbox->addWidget(btnGtr);
QObject::connect(btnGtr, SIGNAL (clicked()), this, SLOT (handleButton()));
}
mainLayout->addWidget(groupBox);
}
void MyWidget::onaddbutton_clicked()
{
pagecounter += 1;
QHBoxLayout *top = new QHBoxLayout;
QGroupBox *groupBox = new QGroupBox[pagecounter];
QPushButton *btnTest = new QPushButton("G", this);
top->addWidget(groupBox);
top->addWidget(btnTest);
top->maximumSize();
mainLayout->addLayout(top);
QVBoxLayout *vbox = new QVBoxLayout;
groupBox->setLayout(vbox);
signalMapper = new QSignalMapper();
for(int i = 0; i<pagecounter; i++){
connect(btnTest, &QPushButton::clicked, [=] {
emit systembutton_clicked(&groupBox[i], vbox, i);
});
signalMapper->setMapping(btnTest, i);
}
}