PDA

View Full Version : Buttons inside QGroupBoxes onclick of QPushButtons.



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);
}
}

anda_skoa
23rd December 2016, 18:30
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,
_

bchinfosieeuw
23rd December 2016, 22:49
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?




void MyWidget::systembutton_clicked(int j, QGroupBox *groupBox, QVBoxLayout *vbox)
{

systemcounter[j] += 1;
QLayoutItem *child;
while ((child = vbox->takeAt(0)) != 0){
delete child;
}
for(int i = 0; i<systemcounter[j]; 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;
systemcounter = new int[pagecounter];
systemcounter[pagecounter-1] = 0;
QHBoxLayout *top = new QHBoxLayout;

QGroupBox *groupBox = new QGroupBox(tr("&Page %1").arg(pagecounter));
QPushButton *btnTest = new QPushButton("G", this);
btnTest->setObjectName(QString::number(pagecounter-1));

top->addWidget(groupBox);
top->addWidget(btnTest);
top->maximumSize();

mainLayout->addLayout(top);

QVBoxLayout *vbox = new QVBoxLayout;
groupBox->setLayout(vbox);
connect(btnTest, &QPushButton::clicked, [=] {
emit systembutton_clicked(sender()->objectName().toInt(), groupBox, vbox);
});
}

bchinfosieeuw
24th December 2016, 01:41
I now found out that it had to be:
btnTest->objectName().toInt()
Thank you.

anda_skoa
24th December 2016, 14:24
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,
_