Buttons inside QGroupBoxes onclick of QPushButtons.
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?
Code:
{
systemcounter += 1;
while ((child = vbox->takeAt(0)) != 0){
delete child;
}
for(int i = 0; i<systemcounter; i++){
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;
top->addWidget(groupBox);
top->addWidget(btnTest);
top->maximumSize();
mainLayout->addLayout(top);
groupBox->setLayout(vbox);
for(int i = 0; i<pagecounter; i++){
connect(btnTest, &QPushButton::clicked, [=] {
emit systembutton_clicked(&groupBox[i], vbox, i);
});
signalMapper->setMapping(btnTest, i);
}
}
Re: Buttons inside QGroupBoxes onclick of QPushButtons.
There are a couple of weird things here.
Are you sure you want to create an array of QGroupBox in onaddbutton_clicked()?
What exactly are your plans for the QSIgnalMapper?
Not sure why you are wondering that btnTest adds things to all groupboxes, after all you connect it that way.
Also no "emit" int that lambda, systembutton_clicked() is not a signal.
Cheers,
_
Re: Buttons inside QGroupBoxes onclick of QPushButtons.
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?
Code:
{
systemcounter[j] += 1;
while ((child = vbox->takeAt(0)) != 0){
delete child;
}
for(int i = 0; i<systemcounter[j]; i++){
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;
systemcounter = new int[pagecounter];
systemcounter[pagecounter-1] = 0;
btnTest
->setObjectName
(QString::number(pagecounter
-1));
top->addWidget(groupBox);
top->addWidget(btnTest);
top->maximumSize();
mainLayout->addLayout(top);
groupBox->setLayout(vbox);
connect(btnTest, &QPushButton::clicked, [=] {
emit systembutton_clicked(sender()->objectName().toInt(), groupBox, vbox);
});
}
Re: Buttons inside QGroupBoxes onclick of QPushButtons.
I now found out that it had to be:
btnTest->objectName().toInt()
Thank you.
Re: Buttons inside QGroupBoxes onclick of QPushButtons.
You could also just set the number as a dynamic property.
And are you sure you want to create a new array every time the first slot is called?
Cheers,
_