PDA

View Full Version : Dynamically add QPushButtons to dynamically added QGroupBoxes



bchinfosieeuw
21st December 2016, 17:31
How do I dynamically add QPushButtons to dynamically added QGroupBoxes? Here is my code (inside a childwindow) which dynamically adds Groupboxes along with their PushButtons. Onclick of one of these PushButtons there must be added PushButtons inside the belonging GroupBox. But how to place them inside the GroupBox?




void MyWidget::systemButton(QGroupBox *groupBox[])
{
systemcounter += 1;

QPushButton *btnGtr = new QPushButton();
btnGtr->setText(QString("Guitar: %1").arg(systemcounter));
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(btnGtr);
groupBox->setLayout(vbox);

mainLayout->addWidget(groupBox);

QObject::connect(btnGtr, SIGNAL (clicked()), this, SLOT (handleButton()));
}

void MyWidget::on_addbutton_clicked()
{
pagecounter += 1;
QHBoxLayout *top = new QHBoxLayout;
QGroupBox *groupBox = new QGroupBox(tr("&Page 1"));
QPushButton *btnTest = new QPushButton();
btnTest->setText(QString("Test: %1").arg(pagecounter));

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

top->maximumSize();

mainLayout->addLayout(top);

QObject::connect(btnTest, SIGNAL (clicked()), this, SLOT (systemButton()));
}

anda_skoa
22nd December 2016, 12:16
I am not exactly sure what you are asking for, can you explain with pointing into your code?

Do you mean that when "btnTest" is clicked, another button should be added to the same button group?

Cheers,
_

bchinfosieeuw
22nd December 2016, 12:38
When on_addbutton_clicked() is triggered, groupBox and its button (btnTest) are added (working). When btnTest is clicked, systemButton() is triggered, which must add a button inside its groupBox. How to do this? I thought of passing QGroupBox as an argument of systemButton, like
QObject::connect(btnTest, SIGNAL (clicked()), this, SLOT (systemButton(QGroupBox)));
but then how to get it working?

Added after 10 minutes:

Doing it this way I get the following errormessage when on_addbutton_clicked() is triggered:
QObject::connect: No such slot MyWidget::systembutton_clicked(QGroupBox) in ..\mywidget.cpp:75
QObject::connect: (receiver name: 'MyWidget')

bchinfosieeuw
22nd December 2016, 15:16
Yes, I would like to add the button to a buttonGroup (inside the GroupBox)

anda_skoa
22nd December 2016, 17:46
QObject::connect(btnTest, SIGNAL (clicked()), this, SLOT (systemButton(QGroupBox)));

That does not work, signal and slot need matching arguments.

Since the clicked() signal has no argument, a slot connected to it can have on either.

If you are in a C++11 capable environment you could connect to a lambda that captures thes groupbox.

If you are not, then simply retrieve the button's parent widget() inside the slot.
See QObject::sender() on how to get the button that triggered the slot.

A third option it to have a special receiver class that knows the groupbox it is responsible for.

Cheers,
_

bchinfosieeuw
22nd December 2016, 22:04
Your first solution works for me. I might ask some additional questions though...